asp.net-mvc-routing

MVC 4 Remove “home” from base route

大憨熊 提交于 2019-11-29 21:24:44
Basically I want to make it so that: http://website.com/Home/About Shows up as: http://website.com/About The "home" controller showing up in the url would make the url longer for the user to read. I tried to do the following: routes.MapRoute( name: "About", url: "", defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional } ); Could someone help me out please? Try something like this: routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "OnlyAction", "{action}", new { controller = "Home", action = "Index" } ); routes.MapRoute( "Default", // Route name "

ASP.NET-MVC . How to get the controller name from an url?

雨燕双飞 提交于 2019-11-29 21:10:42
问题 How can I get the controller name of a relative Url, using the routes I have defined in Global.asax? Example: if I have a route defiend like this: routes.MapRoute( "Default", // Route name "{language}/{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "", language = "en" } from the string "~/en/products/list" I want to have products (the controller name). Is there any existing method that already does this? 回答1: See Stephen Walther's blog

Difference between “MapHttpRoute” and “MapRoute”?

断了今生、忘了曾经 提交于 2019-11-29 20:22:58
Why using "MapRoute" for "Default" routing, while using "MapHttpRoute" for "DefaultApi" routing? routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}" ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); 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

MVC Custom Routing Subdomain

老子叫甜甜 提交于 2019-11-29 15:58:24
I'm trying to build a "Tenant" Subdomain route that attaches to a MVC Area. In this case I have an Area called "Tenant" which has two controllers; Public and Admin. My custom Route is used to grab the Subdomain if it matches then route them to the proper Controller-Action-Area. The base of this project came from the following http://www.matrichard.com/post/asp.net-mvc-5-routing-with-subdomain The problem I'm having is in the custom Subdomain Route. When I hit the Public/Index Route, the routeData is returning null and I see the following error. Although if the route is /admin it returns the

ASP.NET MVC Dynamic Routes and Action Links with Arbitrary Depth [duplicate]

元气小坏坏 提交于 2019-11-29 15:52:42
问题 This question already has answers here : asp.net mvc complex routing for tree path (4 answers) Closed 5 years ago . I'd like to put together a forum/message board with ASP.NET MVC. Pretty common on these types of forums are hierarchical board categories, so for instance: -General Discussion -Technical Support --Website Technical support --Product Technical Support ---Product A Technical Support ---Product B Technical Support Below each category are then topics and messages belong to those

ASP.NET MVC5/6 Routing based on Http Header values

岁酱吖の 提交于 2019-11-29 14:45:11
Let's say I have a most basic controller public class HomeController : Controller { public ActionResult Index(string id, string language) { return View(); } } Which takes in 2 parameters. However there is one requirement that the client which calls the action method should pass id value from URL but language value from http header. It means the url should be /Home/Index/12345 and meanwhile the calling client will set a Http Header value language : en . How shall I set the routing in MVC5 or MVC6 to achieve the requirement? Please don't provide samples from Web Api. Thanks There is an attribute

Is there a way to have a RoutePrefix that starts with an optional parameter?

给你一囗甜甜゛ 提交于 2019-11-29 14:05:36
I want to reach the Bikes controller with these URL's: /bikes // (default path for US) /ca/bikes // (path for Canada) One way of achieving that is using multiple Route Attributes per Action: [Route("bikes")] [Route("{country}/bikes")] public ActionResult Index() To keep it DRY I'd prefer to use a RoutePrefix, but multiple Route Prefixes are not allowed: [RoutePrefix("bikes")] [RoutePrefix("{country}/bikes")] // <-- Error: Duplicate 'RoutePrefix' attribute public class BikesController : BaseController [Route("")] public ActionResult Index() I've tried using just this Route Prefix: [RoutePrefix(

@Url.Action getting ?Length=2 appended

末鹿安然 提交于 2019-11-29 13:56:45
I have this at the top of each of several translations of the "Terms of Use" page: <li><a href="@Url.Action("Index", "Terms")">English</a></li> <li><a href="@Url.Action("Index", "Terms", "de")">Deutsch</a></li> <li><a href="@Url.Action("Index", "Terms", "fr")">Français</a></li> <li><a href="@Url.Action("Index", "Terms", "it")">Italiano</a></li> <li><a href="@Url.Action("Index", "Terms", "nl")">Nederlands</a></li> <li><a href="@Url.Action("Index", "Terms", "hu")">Maygar</a></li> <li><a href="@Url.Action("Index", "Terms", "es")">Español</a></li> <li><a href="@Url.Action("Index", "Terms", "zh")"

How do I enable special characters in MVC routing?

痴心易碎 提交于 2019-11-29 13:51:15
I'm using asp.net MVC 4. These are my routes: routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}" ); My current controller responds to the following request correctly: http://localhost:2020/PrivacyUrls/Details/ct14524 How can I validate urls like these? http://localhost:2020/PrivacyUrls/Details/*ct14524 http://localhost:2020/PrivacyUrls/Details/&ct14524 which now returns 404. A potentially dangerous Request.Path value was detected from the client (*). A potentially dangerous Request.Path value was detected from the client (&). I thought adding this route, but it didn't help:

Using Url.RouteUrl() with Route Names in an Area

房东的猫 提交于 2019-11-29 13:09:51
As a side note, I understand the whole ambiguous controller names problem and have used namespacing to get my routes working, so I don't think that is an issue here. So far I have my project level controllers and then a User Area with the following registration: public class UserAreaRegistration : AreaRegistration { public override string AreaName { get { return "User"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "UserHome", "User/{id}", new { action = "Index", controller = "Home", id = 0 }, new { controller = @"Home", id = @"\d+" } ); context