asp.net-mvc-routing

ASP.NET MVC Routes: How do I omit “index” from a URL

喜欢而已 提交于 2019-12-02 04:23:43
I have a controller called "StuffController" with a parameterless Index action. I want this action to be called from a URL in the form mysite.com/stuff My controller is defined as public class StuffController : BaseController { public ActionResult Index() { // Return list of Stuff } } I added a custom route so the routes are defined like this: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // Custom route to show index routes.MapRoute( name: "StuffList", url: "Stuff", defaults: new { controller = "Stuff", action = "Index" } );

How do I 301 redirect /Home to root?

我是研究僧i 提交于 2019-12-02 02:19:31
Here is my route in Global.asax to remove /Home: routes.MapRoute("Root", "{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); Well I need to setup a 301 redirect because someone linked to /Home and they're getting a 404. So how do I setup the 301? I checked the way the route was setting up and it is looking for a "Home" action method in the "Home" controller. So obviously I could add: public ActionResult Home() { Response.Status = "301 Moved Permanently"; Response.RedirectLocation = "/"; Response.End(); return Redirect("~/"); } However, there's gotta be

Ignore first segments in ASP.NET MVC Core routing

我们两清 提交于 2019-12-02 01:45:38
问题 I'm looking for a route definition that is able to match routes like: /segment/xxxx/def /segment/.../xxxx/def /segment/that/can/span/xxxx/def and be able to run the action xxxx with param `def. But this kind of route isn't allowed: [Route("/{*segment}/xxx/{myparam}")] How can it be done? 回答1: You can use a custom IRouter combined with a regular expression to do advanced URL matching such as this. public class EndsWithRoute : IRouter { private readonly Regex urlPattern; private readonly string

Ignore first segments in ASP.NET MVC Core routing

岁酱吖の 提交于 2019-12-02 01:28:43
I'm looking for a route definition that is able to match routes like: /segment/xxxx/def /segment/.../xxxx/def /segment/that/can/span/xxxx/def and be able to run the action xxxx with param `def. But this kind of route isn't allowed: [Route("/{*segment}/xxx/{myparam}")] How can it be done? You can use a custom IRouter combined with a regular expression to do advanced URL matching such as this. public class EndsWithRoute : IRouter { private readonly Regex urlPattern; private readonly string controllerName; private readonly string actionName; private readonly string parameterName; private readonly

trailing slash issue in IIS 8.5

狂风中的少年 提交于 2019-12-02 01:09:43
We are moving a ASP.NET project from IIS6 (Win Server 2003) to IIS 8.5(Win Server 2012 R2). The project has some MVC components for which the following routing is used. routes.MapRoute("simple", "{controller}.mvc/{action}/{id}"); routes.MapRoute( "Default", "{controller}.mvc/{action}/{id}" new { controller = "Home", action = "Index", id = "" } ); Thus call to MyDemoController would be accessed by MyDemo.mvc Now what happens, when I use the url as MyDemo.mvc/ it works, but when I use MyDemo.mvc without the slash it throws 404 error. This happens only in the deployed server. In our local

RequestContext - RouteData does not contain action

陌路散爱 提交于 2019-12-02 00:57:09
So i've created my own ControllerFactory and i'm overloading GetControllerSessionBehavior in order to extend the MVC behavior. To do my custom work i have to use reflection on the called action. However i've stumbled upon a weird issue - i can't retrieve the action by accessing RequestContext.RouteData While setting up a reproduction sample for this i was not able to reproduce the error. Is anyone aware of possible reasons for this or knows how to retrieve the action by calling a method with the request context other than this? public class CustomControllerFactory : DefaultControllerFactory {

How to make MVC Routing handle url with dashes

自作多情 提交于 2019-12-02 00:30:32
问题 I have a route defined as follows in MVC: routes.MapRoute( name: "ContentNavigation", url: "{viewType}/{category}-{subCategory}", defaults: new { controller = "Home", action = "GetMenuAndContent", viewType = String.Empty, category = String.Empty, subCategory = String.Empty }); If I navigate to http://example.com/something/category-and-this-is-a-subcategory It fills the variables as: viewType: "something" category: "category-and-this-is-a" subCategory: "subcategory". What I want is for the

How do I generically implement URL-rewriting in a MapRoute method?

為{幸葍}努か 提交于 2019-12-02 00:09:26
I am attempting to rewrite URL's from C#'s Pascal-case to SEO-friendly format. For example, I want something like /User/Home/MyJumbledPageName to look like this: /user/home/my-jumbled-page-name // lower-case, and words separated by dashes Here is my method for converting each "token" in the URL: public static string GetSEOFriendlyToken(string token) { StringBuilder str = new StringBuilder(); for (int i = 0, len = token.Length; i < len; i++) { if (i == 0) { // setting the first capital char to lower-case: str.Append(Char.ToLower(token[i])); } else if (Char.IsUpper(token[i])) { // setting any

Get a complete url like http://google.com as action input

点点圈 提交于 2019-12-01 23:32:44
问题 I want to get url in my action to return the page's PageRank. I have a route like this: routes.MapRoute( name: "PRURL", url: "Utility/PR/{*url}", defaults: new { controller = "Utility", action = "PR", url = UrlParameter.Optional } ); but whenever I go to http://localhost:1619/Utility/PR/http://google.com I just get a System.Web.HttpException that tell me A potentially dangerous Request.Path value was detected from the client (:). I want to resolve this problem but I don't know how! can

Current URL in ASPCore Middleware?

不羁的心 提交于 2019-12-01 22:26:34
Is there a way I can access the current Requested URL in ASPCore 2.0 Middleware? Is there something I can Inject? Shyju HttpContext object will be passed to the Invoke method of your middleware. You can access the Request property on that. You can use the GetDisplayUrl extension method or GetEncodedUrl extension method. public Task Invoke(HttpContext context) { var url1 =context.Request.GetDisplayUrl(); var url2 = context.Request.GetEncodedUrl(); // Call the next delegate/middleware in the pipeline return this._next(context); } These 2 extension methods are defined in Microsoft.AspNetCore.Http