asp.net-mvc-routing

MVC 4 Remove “home” from base route

主宰稳场 提交于 2019-11-28 16:42:39
问题 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? 回答1: Try something like this: routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "OnlyAction"

ASP.NET MVC - Extract parameter of an URL

早过忘川 提交于 2019-11-28 16:29:27
I'm trying to extract the parameters of my URL, something like this. /Administration/Customer/Edit/1 extract: 1 /Administration/Product/Edit/18?allowed=true extract: 18?allowed=true /Administration/Product/Create?allowed=true extract: ?allowed=true Someone can help? Thanks! David Glenn 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

ASP.NET MVC 5 (Visual Studio 2013 Preview) Change Login Url for [Authorize]

六眼飞鱼酱① 提交于 2019-11-28 14:04:12
Hey guys I started playing around with the ASP.NET MVC 5 preview and everything has been going fine so far (I can only recommend it). However, I wonder where I can set the Login-Url for the Built-In [Authorize] -Attribute. I've moved the AccountController to a an area, so the path to the Login action is no longer /Account/Login but MyArea/Account/Login , which is ignored by the [Authorize] -Attribute, which in turn means, that whenever one navigates to a controller or action with the attribute set, one is redirected to the wrong path /Account/Login . Look in web.config for a section like this:

ASP.NET MVC Route with values before the controller and no trailing slash

折月煮酒 提交于 2019-11-28 13:56:22
This is probably a simple question but I just can't get it to work. I've got this route specified in my RouteConfig routes.MapRoute( name: "DefaultSiteRoute", url: "{accountid}/{hostname}/{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, accountid = UrlParameter.Optional, hostname = UrlParameter.Optional } ); And it works fine for a url like this /123456/www.test.com/ or this /123456/www.test.com/Controller/Action but it can't cope with this /123456/www.test.com I get an IIS 404 What is stranger is if I call Url.Action for that

How can I get controller type and action info from a url or from route data?

徘徊边缘 提交于 2019-11-28 12:06:23
How can I get the controller action (method) and controller type that will be called, given the System.Web.Routing.RouteData ? My scenario is this - I want to be able to do perform certain actions (or not) in the OnActionExecuting method for an action. However, I will often want to know not the current action, but the "root" action being called; by this I mean I may have a view called "Login", which is my login page. This view may include another partial view "LeftNav". When OnActionExecuting is called for LeftNav, I want to be able to determine that it is really being called for the "root"

Dynamically modify RouteValueDictionary

◇◆丶佛笑我妖孽 提交于 2019-11-28 11:37:58
As a followup of this question , I'm trying to implement a custom route constraint in order to dynamically modify the RouteValueDictionary for a specific route. I'm 95% of the way there: I can match any route based on the supplied parameter pattern matching the action specified in the url. The very last step of this process is to overwrite the RouteValueDictionary to rename the keys as the appropriate parameters for the controller action. Here's the relevant snippet of my code (the entire class is almost 300 lines so I don't want to add all of it, but I can if needed): public class

Routing a url with extension in MVC4 won't work, tries to serve up static file

倖福魔咒の 提交于 2019-11-28 10:57:16
I'm using MVC4 and need to route a request like this to a controller: [myapp]/data/fileinfo.xml Here is the route I have configured: routes.MapRoute( name: "Data", url: "Data/{file}", defaults: new { controller = "Data", action = "fileinfo"} ); Now, this works perfectly fine and routes requests to my DataController if the URL does not include the .xml extension, but as soon as an extension is used, IIS tries to serve up a static file (instead of routing to my controller) and I get a 404 error. I've read loads of questions/answers about this issue online, and every solution I've tried has

Generating Route Url to MVC controller action from WebAPI

Deadly 提交于 2019-11-28 10:45:49
I'm used to generating route URLs to other controller actions within an MVC controller action using something similar to below: public class ApplicationController : Controller { public ActionResult Index( ) { var url = Url.RouteUrl("routename", new { controller = "Application", Action = "Index2" , other="other" }); } public ActionResult Index2( string other) { } } But I also need to be able to generate URLs to MVC controller actions from within webapi too, How would I go about doing this? There seems to be a UrlHelper property on the APIController but I cant find any examples of how to use

How can I check if a route (ASP.NET MVC) exists for a given path? [duplicate]

旧时模样 提交于 2019-11-28 09:50:30
This question already has an answer here: How to get RouteData by URL? 2 answers I have a list of local URL’s and I need to determine if they are “valid MVC paths”. How can I check if a URL (path) maps to a MVC controller? Phil Haack's Route Debugger will find a route that match the current request and does so using the current HttpContext. I would like to get this info without building up a mock HttpContext - if possible. You can call RouteTable.Routes.GetRouteData with a mocked HttpContextBase. The routes are matched internally using the request's AppRelativeCurrentExecutionFilePath .

ASP.NET MVC Default route?

Deadly 提交于 2019-11-28 09:04:51
I created a new ASP.NET MVC project and implemented a site authorization filter. When I map the routes to the {controller}/{action} pair, I pass a role = "SomeRole" default to the route. It works perfectly if I go through the full url ( http://localhost/somecontroller/someaction ) and I specified the full route MapRoute("SomeAction", "somecontroller/someaction", new { controller = "SomeController", action = "SomeAction", role = "SomeRole"); The problem is that when somebody visits http://thesiteaddress.com there has to be a default route that invokes /home/index instead of / and if I specify