asp.net-mvc-routing

ASP.NET MVC 4 Routes - controller/id vs controller/action/id

微笑、不失礼 提交于 2019-11-27 01:51:29
问题 I'm trying to add a route to the default one, so that I have both urls working: http://www.mywebsite.com/users/create http://www.mywebsite.com/users/1 This will make the first route work: routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "users", action = "Index", id = UrlParameter.Optional } ); However, the second route won't work obviously. This will make the second route work, but will break the first one: routes.MapRoute( name: "Book", url:

RedirectToAction and RedirectToRoute

无人久伴 提交于 2019-11-27 01:49:18
问题 In my controller of webpage 1, I want to redirect to Webpage 2, passing 2 variables. I tried using RedirectToRoute, but cannot get it to work; wrong URL is displayed. I then switched to using RedirectToAction. my code: Routing routes.MapRoute( "CreateAdditionalPreviousNames", // Route name "Users/{controller}/{action}/{userId}/{applicantId}", // URL with parameters new { controller = "UsersAdditionalPreviousNames", action = "Index", userId = UrlParameter.Optional, applicantId = UrlParameter

ASP.net MVC routing with optional first parameter

China☆狼群 提交于 2019-11-27 01:40:55
问题 I need to provide following functionality for one of the web sites. http://www.example.com/ [sponsor] /{controller}/{action} Depending on the [sponsor], the web page has to be customized. I tried combination of registering the routes with Application_Start and Session_Start but not able to get it working. public static void RegisterRoutes(RouteCollection routes, string sponsor) { if (routes[sponsor] == null) { routes.MapRoute( sponsor, // Route name sponsor + "/{controller}/{action}/{id}", //

MVC WEB API routing fails when url contains encoded ampersand

孤街浪徒 提交于 2019-11-27 01:34:24
问题 When i call my webservice witch takes two parameters i get: A potentially dangerous Request.Path value was detected from the client (&). Routeconfig: config.Routes.MapHttpRoute( name: "PropertiesSearch", routeTemplate: "api/property/Search/{category}/{query}", defaults: new { controller = "Property", action = "Search", category = "common", query = string.Empty } ); Controllermethod: [HttpGet] public SearchResult Search(string category, string query) { } When i call the api with: /api/property

ASP.NET MVC 4 - 301 Redirects in RouteConfig.cs

一笑奈何 提交于 2019-11-27 00:42:27
问题 How can I add a route to the RouteConfig.cs file in an ASP.NET MVC 4 app to perform a permanent 301 redirect to another route? I would like certain different routes to point at the same controller action - it seems a 301 would be best practice for this, specially for SEO? Thanks. 回答1: You have to use RedirectPermanent, here's an example: public class RedirectController : Controller { public ActionResult News() { // your code return RedirectPermanent("/News"); } } in the global asax: routes

What is routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”)

别来无恙 提交于 2019-11-27 00:33:25
What is routes.IgnoreRoute("{resource}.axd/{*pathInfo}") I cannot find any .axd file in my project, can I remove this route rule? .axd files don't exist physically. ASP.NET uses URLs with .axd extensions (ScriptResource.axd and WebResource.axd) internally, and they are handled by an HttpHandler. Therefore, you should keep this rule, to prevent ASP.NET MVC from trying to handle the request instead of letting the dedicated HttpHandler do it. Some Background If you open up this file: %WINDIR%\Microsoft.NET\Framework\version\Config\Web.config you will find this within the file: <add path=

How do I route a URL with a querystring in ASP.NET MVC?

我只是一个虾纸丫 提交于 2019-11-26 23:02:59
I'm trying to setup a custom route in MVC to take a URL from another system in the following format: ../ABC/ABC01?Key=123&Group=456 The 01 after the second ABC is a step number this will change and the Key and Group parameters will change. I need to route this to one action in a controller with the step number key and group as paramters. I've attempted the following code however it throws an exception: Code: routes.MapRoute( "OpenCase", "ABC/ABC{stepNo}?Key={key}&Group={group}", new {controller = "ABC1", action = "OpenCase"} ); Exception: `The route URL cannot start with a '/' or '~' character

How to get RouteData by URL?

醉酒当歌 提交于 2019-11-26 22:28:57
I need to get RoutData by given URL string in ASP.NET MVC application. I've found the way that I need to mock HttpContextBase based on my URL string and then pass it to RouteTable.Routes.GetRouteData() method in Route Parsing (Uri to Route) thread. How to mock HttpContextBase to retrieve RouteData by URL string using RouteTable.Routes.GetRouteData() ? Or is there another way to retrieve RouteData by URL? Serge S. I used Moq to determine what members of HttpContextBase are used in GetRouteData() . They are: Request AppRelativeCurrentExecutionFilePath PathInfo Request

How can I create a route constraint of type System.Guid?

若如初见. 提交于 2019-11-26 22:16:42
问题 Can anyone point me in the right direction on how to map a route which requires two guids? ie. http://blah.com/somecontroller/someaction/{firstGuid}/{secondGuid} where both firstGuid and secondGuid are not optional and must be of type system.Guid? 回答1: Create a RouteConstraint like the following: public class GuidConstraint : IRouteConstraint { public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { if

ASP.Net MVC3 routing reserved words?

大憨熊 提交于 2019-11-26 22:02:05
问题 I have an ASP.Net MVC application with the standard routes defined. The app edits meta data for our database. The url scheme is: http://localhost/tables/Edit/[Name of Table Here] This calls the edit function on the tables controller and passes in the name of the table as the parameter id . All of the tables work fine except one named con . The following URL results in a 404: http://localhost/tables/Edit/con The only thing I can think of is that con must be some sort of reserved word with