asp.net-mvc-routing

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

早过忘川 提交于 2019-11-28 08:57:01
问题 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

How to access the current HttpRequestMessage object globally?

断了今生、忘了曾经 提交于 2019-11-28 08:53:25
I have a method which creates an HttpResponseMessage containing an Error object which will be returned based on the current request media type formatter. Currently, I have hardcoded the XmlMediaTypeFormatter but I'd like to be able to find the current request MediaTypeFormatter at runtime but I don't have access to the current request object since my below code exists on a separate class library. private HttpResponseMessage Create(HttpStatusCode statusCode, string errorCode, string errorMessage) { var result = new HttpResponseMessage(statusCode) { Content = new ObjectContent<Error>(new Error()

AreaRegistration.RegisterAllAreas() is not Registering Rules For Area

大憨熊 提交于 2019-11-28 08:38:58
I have an MVC 4 web application which consists some areas. I have a problem with the routing rules of an area named "Catalog". The RouteConfig.cs file is: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, ); } and Global.asax as follows: protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig

@Url.Action getting ?Length=2 appended

谁都会走 提交于 2019-11-28 07:43:22
问题 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="

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

无人久伴 提交于 2019-11-28 07:33:24
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: "books/{id}", defaults: new { controller = "users", action = "Details" } ); How to combine the two

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

China☆狼群 提交于 2019-11-28 07:33:01
问题 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 :

RedirectToAction and RedirectToRoute

孤者浪人 提交于 2019-11-28 07:27:29
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.Optional } // Parameter defaults ); RedirectToAction (which works) return RedirectToAction("Index",

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

半城伤御伤魂 提交于 2019-11-28 07:02:38
问题 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 {

ASP.net MVC routing with optional first parameter

笑着哭i 提交于 2019-11-28 07:02:12
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}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } //

MVC WEB API routing fails when url contains encoded ampersand

岁酱吖の 提交于 2019-11-28 06:53:06
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/search/homes/areaId%3D20339%26areaId%3D20015 A potentially dangerous Request.Path value was detected