asp.net-mvc-routing

MVC 4: Custom Route & Html.Action out of synch

被刻印的时光 ゝ 提交于 2019-12-10 12:23:39
问题 So I have this custom route, which sets up the route table based on culture in the URL, but when I call Url.Action(...), it does not generate the localized URL. Any ideas what I'm doing wrong? The culture is changing on the page and I am able to determine what language user has selected, but Url.Action is not generating localized URL.. This is the custom route, which changes the route table values (not sure if this standard way of doing it): public class CultureRoute : Route { public

Map url text to boolean parameter with MVC attribute routing

依然范特西╮ 提交于 2019-12-10 11:52:16
问题 Given the following two urls: /employee/list/active /employee/list/inactive How do I map the active/inactive part of the url to a boolean action method parameter, active being true, inactive being false? [Route("employee/list")] public ActionResult List(bool? active = null) 回答1: The enum is a correct approach as it allows you to easily add new statuses in the future : [Route("employee/list/{status}")] public ActionResult List(status status) { ... } public enum status { active, inactive } Even

ASP.NET MVC Overriding Index action with optional parameter

跟風遠走 提交于 2019-12-10 11:25:51
问题 I want to accept /User/ and /User/213123 Where 213123 is a parameter (user_id) Here is my RouteConfig.cs : routes.MapRoute( name: "user", url: "{controller}/{action}/{username}", defaults: new { controller = "User", action = "Index", username = UrlParameter.Optional } ); And my UserController.cs : public ActionResult Index() { ViewData["Message"] = "user index"; return View(); } [Route("user/{username}")] public ActionResult Index(string username) { ViewData["Message"] = "!" + username + "!";

Route patterns vs individual routes

[亡魂溺海] 提交于 2019-12-10 11:05:48
问题 Currently I have a controller which looks something like this: public class MyController : Controller { public ActionResult Action1 (int id1, int id2) { } public ActionResult Action2 (int id3, int id4) { } } As you can see both my controllers have the same parameter "pattern", two non-nullable signed integers. My route config looks like this: routes.MapRoute( name: "Action2", url: "My/Action2/{id3}-{id4}", defaults: new { controller = "My", action = "Action2", id3 = 0, id4 = 0 } ); routes

Hide one controller name from mvc url, show other controller names

倾然丶 夕夏残阳落幕 提交于 2019-12-10 10:29:56
问题 I have two controllers, HomeController and ResourcesController. I want to hide the Home/ from the url when action on HomeController is requested, but for ResourcesController (or any other controlelr) I want to keep the controller name in url. E.g. /Home/Products will be /Produtcs, but /Resources/Banana should stay /Resources/Banana These are my routes: routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "SpecificRoute", url: "{action}/{id}", defaults: new { controller =

Implementing Routes for dynamic actions in MVC 4.0

落花浮王杯 提交于 2019-12-10 09:46:32
问题 Is it possible to define a route in MVC that dynamically resolves the action based on part of the route? public class PersonalController { public ActionResult FriendGroup() { //code } public ActionResult RelativeGroup() { //code } public ActionResult GirlFriendGroup() { //code } } I want to implement a routing for my Group Action Method below Url: www.ParlaGroups.com/FriendGroup www.ParlaGroups.com/RelativeGroup www.ParlaGroups.com/GirlFriendGroup routes.MapRoute( "Friends", "/Personal/

.NET MVC routing - catchall at start of route?

Deadly 提交于 2019-12-10 04:09:52
问题 Is there any way I can match: /a/myApp/Feature /a/b/c/myApp/Feature /x/y/z/myApp/Feature with a route that doesn't specifically know what the path before myApp/Feature is? What I essentially want to do is: RouteTable.Routes.MapRoute( "myAppFeatureRoute", "{*path}/myApp/Feature", new { controller = "myApp", action = "Feature" }); but you can't put a catchall at the start of a route. If I just try "{path}/myApp/Feature", that will match "/a/myApp/Feature" but not "/a/b/c/myApp/Feature". I tried

Getting MVC to ignore route to site root

走远了吗. 提交于 2019-12-10 03:06:11
问题 I'm working on a site that is partially static content and partially MVC. The root of the site is index.html and I have all of the controllers explicitly routed and all html files ignored. However, when you hit the root of the website, it tries to route it. How can I tell the route engine to ignore the root of the site? www.mysite.com should not be routed, but instead go to index.html. Here is my routing configuration: routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("*

Should I use RouteParameter or UrlParameter for an Asp.NET web-api route?

混江龙づ霸主 提交于 2019-12-10 01:52:32
问题 I've seen both being used and so I wonder, do they do the same thing or different things? If it's the latter, what's the difference? I tried answering it myself by having a look at the visual studio MVC 4 (rc) web api template, but sadly it uses both, so my confusion remains. Here's what the template contains: public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapHttpRoute( name: "DefaultApi",

Unexpected route chosen while generating an outgoing url

吃可爱长大的小学妹 提交于 2019-12-10 01:02:44
问题 Please, consider the following routes: routes.MapRoute( "route1", "{controller}/{month}-{year}/{action}/{user}" ); routes.MapRoute( "route2", "{controller}/{month}-{year}/{action}" ); And the following tests: TEST 1 [TestMethod] public void Test1() { RouteCollection routes = new RouteCollection(); MvcApplication.RegisterRoutes(routes); RequestContext context = new RequestContext(CreateHttpContext(), new RouteData()); DateTime now = DateTime.Now; string result; context.RouteData.Values.Add(