asp.net-mvc-routing

How to find the right route in a RouteCollectionRoute?

馋奶兔 提交于 2019-11-30 18:16:30
I am testing ASP MVC routes . I am having an issue with attribute routes in ASP MVC 5.1 When I have a controller like this: public class FooController : Controller { [Route("foo")] [HttpGet] public ActionResult Get() { .... } [Route("foo")] [HttpPost] public ActionResult Post() { .... } } Then in order to test which route matches a particular request, I call routes.GetRouteData . I get a System.Web.Routing.RouteData that contains the route as well as values that should say which controller and action are matched. The problem is that this route is now an instance of RouteCollectionRoute this

ASP.NET MVC routing with one mandatory parameter and one optional parameter?

南笙酒味 提交于 2019-11-30 16:44:13
问题 I've been working on a large MVC application over the past month or so, but this is the first time I've ever needed to define a custom route handler, and I'm running into some problems. Basically I have two parameters to pass. The first one is required and the second one is optional. I'm following this answer here. Here is my custom route: routes.MapRoute( "MyRoute", "{controller}/{action}/{param1}/{param2}", new { controller = "MyController", action = "MyAction", param1 = "", param2 = "" //

ASP.NET Routing: How to make routeConfig handle a more dynamic URL structure

别说谁变了你拦得住时间么 提交于 2019-11-30 16:03:50
My scenario is as follows: a venue can be part of multiple categories and users can also add filters on multiple category types, so my URLs now are like: /venues/beaches/boats/themeparks (this will display all venues that are beaches AND boats AND themeparks) /venues/beaches/boats /venues etc. So the number of different venue types (beaches, boats, themeparks etc) is both dynamic AND optional. I decided to go with URLRouting so I can start to get the different values in codebehind via Request.GetFriendlyUrlSegments() I installed https://www.nuget.org/packages/Microsoft.AspNet.FriendlyUrls.Core

Correct 404 message for a missing ASP.Net MVC controller

删除回忆录丶 提交于 2019-11-30 15:48:45
I have an MVC 2 application that should always give a 'nice' 404 page. However currently I get a low level .Net one: "Server Error in '/sitename' Application..." I have a base controller that has a NotFound action that will render the nice 404 page. Missing actions are handled: protected override void HandleUnknownAction(string actionName) { this.NotFound(actionName).ExecuteResult(this.ControllerContext); } So a visit to {site}/ValidController/NotAnAction gets routed correctly. However a visit to {site}/NotAController doesn't. I have routes set up with a catch all: routes.MapRoute( "MVC routes

Howto automatically add a specific value from current route to all generated links?

前提是你 提交于 2019-11-30 15:41:40
I have site culture in URLs like this: routes.MapRoute( "Default", "{language}/{controller}/{action}/{id}", languageDefaults, languageConstraints) And it works like a charm with a little help from custom MvcHttpHandler that sets current thread's UI culture on every request based on route value. My problem is how do I automatically add the language route value from current request to all outgoing links? E.g. when page /EN/Foo/Bar is requested, I would like this <%=Html.ActionLink( "example link", MVC.Home.Index()) %> To automatically generate the same result as this: <%=Html.ActionLink(

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

℡╲_俬逩灬. 提交于 2019-11-30 14:42:12
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? Laurel.Wu See Stephen Walther's blog post ASP.NET MVC Tip #13 – Unit Test Your Custom Routes The project MvcFakes has an old System.Web

ASP.NET MVC Areas: How to hide “Area” name in URL?

家住魔仙堡 提交于 2019-11-30 12:44:28
问题 When running the MVC 2 Areas example that has a Blog Area and Blog Controller the URL looks like this: http://localhost:50526/Blog/Blog/ShowRecent in the format: RootUrl / AreaName / ControllerName / ActionName Having just discovered MVC Areas, it seem like a great way to organise code, ie create an Area for each section, which in my case each section has its own controller. This means that each AreaName = ControllerName. The effect of this is the double AreaName/ControllerName path in the

Action Parameter Naming

家住魔仙堡 提交于 2019-11-30 11:50:12
问题 Using the default route provided, I'm forced to name my parameters "id". That's fine for a lot of my Controller Actions, but I want to use some better variable naming in certain places. Is there some sort of attribute I can use so that I can have more meaningful variable names in my action signatures? // Default Route: routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" } // Parameter defaults ); /

.NET 4.5 MVC RouteCollection.LowercaseUrls breaks when using Area

谁说我不能喝 提交于 2019-11-30 11:13:31
A new property to RouteCollection was added with .NET Framework 4.5: http://msdn.microsoft.com/en-us/library/system.web.routing.routecollection.lowercaseurls.aspx This works great, until you add an Area to your project. All Urls are propercase again. Any ideas why this happens? It's simple to reproduce: Create new MVC 4 Internet Application (.NET 4.5) Start RouteConfig.RegisterRoutes function with: routes.LowercaseUrls = true; Run the project and you'll see all generated Urls are now lowercase. Add an Area to the project. Run the project again and you'll see all Urls are propercase again! It

Difference between “MapHttpRoute” and “MapRoute”?

馋奶兔 提交于 2019-11-30 10:26:19
问题 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 } ); 回答1: 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,