ASP.NET MVC 6 Tutorial :: Creating Routes With ASP.NET MVC 6

ASP.NET MVC 6 Tutorial | When using MVC 6, you don’t create your Route collection yourself. So in this tutorial, I will show you how to create routes with ASP.NET MVC 6.

You let MVC create the route collection for you. And now, write the following code:

using Microsoft.AspNet.Builder;
using Microsoft.Framework.DependencyInjection;
namespace RoutePlay
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app)
{
app.UseMvc();
}
}
}

The ConfigureServices() method is utilized to enroll MVC with the Dependency Injection framework built into ASP.NET 5. The Configure() system is utilized to register MVC with OWIN. This is what my MVC 6 ProductsController resembles:

Notice that I have not configured any routes. I have not utilized either tradition based or property based directing, yet I don’t have to do this. If I enter the request “/products/index” into my browser address bar then I get the response “It Works!”:

When you calling the ApplicationBuilder.UseMvc() in the Startup class, the MVC framework will add routes for you automatically. The following code will show you, what the framework code for the UseMvc() method looks like:

public static IApplicationBuilder UseMvc([NotNull] this IApplicationBuilder app)
{
return app.UseMvc(routes =>
{
});
}
public static IApplicationBuilder UseMvc(
[NotNull] this IApplicationBuilder app,
[NotNull] Action<IRouteBuilder> configureRoutes)
{
// Verify if AddMvc was done before calling UseMvc
// We use the MvcMarkerService to make sure if all the services were added. MvcServicesHelper.ThrowIfMvcNotRegistered(app.ApplicationServices);
var routes = new RouteBuilder
{
DefaultHandler = new MvcRouteHandler(),
ServiceProvider = app.ApplicationServices
};
configureRoutes(routes);
// Adding the attribute route comes after running the user-code because
// we want to respect any changes to the DefaultHandler.
routes.Routes.Insert(0, AttributeRouting.CreateAttributeMegaRoute(
routes.DefaultHandler,
app.ApplicationServices));
return app.UseRouter(routes.Build());
}

The AttributeRouting.CreateAttributeMegaRoute() does all of the heavy-lifting here (the word “Mega” in its name is very appropriate). The CreateAttributeMegaRoute() method iterates through all of your MVC controller actions and builds routes for you automatically. Now, you can use convention-based routing with ASP.NET MVC 6 by defining the routes in your project’s Startup class. And here is the example code:

using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.DependencyInjection;
namespace RoutePlay
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app)
{
app.UseMvc(routes =>
{
// route1
routes.MapRoute(
name: "route1",
template: "super",
defaults: new { controller = "Products", action = "Index" }
);
// route2
routes.MapRoute(
name: "route2",
template: "awesome",
defaults: new { controller = "Products", action = "Index" }
);
});
}
}
}

I hope this tutorial works for you!

HostForLIFE.eu ASP.NET MVC 6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.

Cheap ASP.NET MVC 6 Hosting :: Cheap and Fast

Cheap ASP.NET MVC 6 Hosting – I Host Azure | Are you looking for cheap ASP.NET MVC 6 hosting? We know you don’t like slow websites. Neither do your visitors. If your site is slow, your visitors will just find another site to buy from or get their information from. That’s why you need cheap yet fast ASP.NET MVC 6 Hosting that suits your need.

Web hosting can seem very complicated when you first think about it. When you break it down into steps, you can understand it easier and it can make you money. Dedicated or shared hosting, which one is right for you? If you run a large website with a lot of traffic, a shared server might limit you and lead to a lot of downtime. Find a dedicated host for more space and bandwidth, even unlimited.

Cheap ASP.NET MVC 6 Hosting – Fastest ASP.NET MVC 6 Hosting

HostForLIFE.eu ASP.NET MVC 6 Hosting currently operates data center located in Amsterdam (Netherlands), London (UK), Washington, D.C. (US), Paris (France), Frankfurt (Germany), Chennai (India), Milan (Italy), Toronto (Canada) and Sao Paulo (Brazil) Data Center. All HostForLIFE.eu ASP.NET MVC 6 data center offers complete redundancy in power, HVAC, fire suppression, network connectivity, and security. Our data center has over 53,000 sq ft of raised floor between the two facilities, HostForLIFE has an offering to fit any need. The datacenter facility sits atop multiple power grids driven by TXU electric, with PowerWare UPS battery backup power and dual diesel generators onsite. Their HVAC systems are condenser units by Data Aire to provide redundancy in cooling coupled with nine managed backbone providers.

img

Cheap ASP.NET MVC 6 Hosting – Cheapest Hosting

HostForLIFE.eu ASP.NET MVC 6 Hosting is European best, cheap and erliable ASP.NET 5 / ASP.NET Core 1.0 Hosting & SQL Server Hosting with instant activation.

HostForLIFE.eu ASP.NET MVC 6 Hosting was established to cater to an under served market in the hosting industry; web hosting for customers who want excellent service. This is why HostForLIFE.eu continues to prosper throughout the web hosting industry’s maturation process.

img

Cheap ASP.NET MVC 6 Hosting – Satisfaction Guaranteed

Guarantees is a promise or assurance, especially one in writing, that something is ofspecified quality, content, benefit, etc., or that it will performsatisfactorily for a given length of time.

HostForLIFE.eu ASP.NET MVC 6 Hosting’s top priority to deliver the ultimate customer experience, and we strongly believe that you’ll love their service . If for any reason you’re unhappy in your first 30 days as a customer, you’re more than welcome to request your money back.