Reputation: 921
I have a project that we're looking to launch with MVC 5 and ASP.NET 5/dnx46. I understand that MVC6 has some changes, but the project will likely be launched prior to MVC6 being out of beta - thus ASP.NET5/MVC5.
The problem I'm having is with routing.
Anything MVC5-based is relying on System.Web, which doesn't appear to play nicely with the rest of the project.
So how does one go about setting up a generic MVC-style route?
I've found some resources that include a ton of boilerplate code, but I can't imagine that MS is requiring everything be written/overridden just to get some basic routing done outside of MVC6?
Upvotes: 1
Views: 516
Reputation: 38477
The following is added to your Startup.cs to get MVC running.
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureRouting(
routeOptions =>
{
// All generated URL's should append a trailing slash.
routeOptions.AppendTrailingSlash = true;
// All generated URL's should be lower-case.
routeOptions.LowercaseUrls = true;
});
services.AddMvc();
}
public void Configure(IApplicationBuilder application)
{
application.UseMvc();
}
Attribute routing is added by default, so you can use the [Route]
and/or [HttpGet]
/[HttpPost]
attributes instead.
[Route("[controller]")] // [controller] is replaced by 'car'.
public class CarController : Controller
{
[Route("hello")] // Or use [HttpGet]
public string World()
{
return "World";
}
}
The old school routing requires a bit more work. You have to add the routes in Startup.cs
application.UseMvc(routes =>
{
routes.MapRoute(
name: "route1",
template: "hello",
defaults: new { controller = "Car", action = "World" });
});
Upvotes: 0
Reputation: 25704
ASP.NET MVC 5 runs only on ASP.NET 4.x.
ASP.NET MVC 6 runs only on ASP.NET 5 (using DNX).
The MVC versions cannot be mixed between the versions of ASP.NET.
ASP.NET MVC 5 and ASP.NET MVC 6 are however still very similar, though they certainly also have some significant changes.
For example, though the routing functionality is largely very similar between MVC 5 and MVC 6, the place where you register the routes is different. Check out the Music Store sample app for how to register routes in an ASP.NET MVC 6 app.
Upvotes: 2