ASP.NET MVC – Extract parameter of an URL

Update RouteData.Values[“id”] + Request.Url.Query Will match all your examples It is not entirely clear what you are trying to achieve. MVC passes URL parameters for you through model binding. public class CustomerController : Controller { public ActionResult Edit(int id) { int customerId = id //the id in the URL return View(); } } public class … Read more

How to use an Area in ASP.NET Core

In order to include an Area in an ASP.NET Core app, first we need to include a conventional route in the Startup.cs file (It’s best to place it before any non-area route): In Startup.cs/Configure method: app.UseMvc(routes => { routes.MapRoute(“areaRoute”, “{area:exists}/{controller=Admin}/{action=Index}/{id?}”); routes.MapRoute( name: “default”, template: “{controller=Home}/{action=Index}/{id?}”); }); Then make a folder named Areas in the app … Read more

Difference between “MapHttpRoute” and “MapRoute”?

If you use Web API on top of ASP.NET they would ultimately both operate on the same underlying ASP.NET route table – however as correctly pointed out, from the user perspective you call two different methods to register route. Routing was designed like this so that when hosting outside of ASP.NET, Web API wouldn’t have … Read more

Force all Areas to use same Layout

You just have to add a file named: _ViewStart.cshtml Under each area views folder: /Areas/Area1/Views/_ViewStart.cshtml And edit the file to point to the root layout like this: @{ Layout = “~/Views/Shared/_Layout.cshtml”; } In order for this to work, you do not have to specify a value in the view’s layout property, if you do, you … Read more

How do you request static .html files under the ~/Views folder in ASP.NET MVC?

I want to be able to request static .html files which are located in the ‘~/Views’ folder. You can’t. There’s a web.config file in this folder which explicitly forbids accessing any file from it. If you want to be able to access files from the client those files should not be placed in the Views … Read more

Set “Homepage” in Asp.Net MVC

Look at the Default.aspx/Default.aspx.cs and the Global.asax.cs You can set up a default route: routes.MapRoute( “Default”, // Route name “”, // URL with parameters new { controller = “Home”, action = “Index”} // Parameter defaults ); Just change the Controller/Action names to your desired default. That should be the last route in the Routing Table.