Frage

I'm working on a project not created by myself. It appears the project was accidentally created as a WebAPI project instead of an MVC project.

I was having issues with the session data not being handled correctly - and this appears due to the fact that the project isn't an MVC project.

Is there any relatively easy way to migrate the project to MVC via modifying references instead of creating a new MVC project and migrating all the files over?

War es hilfreich?

Lösung

You can create ASP.NET MVC controllers and views in the Web API project itself. Just inherit your controller from the Controller class (in System.Web.mvc).

If you create a new Web API project (with VS 2012, MVC4) you can see that you have both a normal MVC controller (HomeController) and an API Controller (ValuesController) in it.

  • MVC Controllers must inherit from Controller class which is System.Web.Mvc
  • API Controllers must inherit from ApiController class which is System.Web.Http

Andere Tipps

The only difference is the controller that you inherit, or you simply inherit from native MVC Controller class. That comment made ​​the route is on file App_Start/WebApiConfig.cs. For example:

namespace MyAplication.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }
}

Class WebApiConfig.cs

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
          //config.Routes.MapHttpRoute(
          //name: "API Default",
          //routeTemplate: "{controller}/{action}/{id}",
          //defaults: new { id = RouteParameter.Optional }
          //);
        }
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top