In my MVC app, why does
Return RedirectToAction("Edit", "Forms", New With {.member = "123"})
return
http://localhost:13/Forms/Edit?member=123
insted of
http://localhost:13/Forms/Edit/123
?
And why does
<%=Html.ActionLink("MyLink", "Edit", "Forms", New With {.member = "123"}, Nothing)%>
do the same thing?
As tvanfosson says, "id" is what the default route engine is set to look for. Anything else as the 3rd param and it will be tacted on as a querystring.
Why? Because of this method in your Global.asax:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
You can change this by adding an additional routes.MapRoute() line, like so:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
routes.MapRoute(
"Default2",
"{controller}/{action}/{member}",
new { controller = "Home", action = "Index", member = "" }
);
The standard routing is set up to use id as the third parameter. Change "member" to "id" and you will get the route that you expect.
Return RedirectToAction("Edit", "Forms", New With { .id = "123"})
Related
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "Teng.Web.Controllers" });
routes.MapRoute(
"CMSArticle",
"{Classify}/{controller}/{action}/{id}",
new { Classify = #"", controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "Teng.Web.Controllers" });
To match CMSArticle http://localhost:4848/ss/home/index/5
I want to http://localhost:4848/ss/home/index
go CMSArticle Routes
home and ss both seems controller names. you have to go for default routes. but before that check your Url.
http://localhost:4848/ss/home/index/5 - Check the ss. normall it comes as
http://localhost:4848/home/index/5
Is classify an actual parameter? I believe they need to be in order of importance. If the route doesn't match to one it falls to the next. Try the below.
routes.MapRoute(
"CMSArticle",
"ss/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "Teng.Web.Controllers" });
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "Teng.Web.Controllers" });
You can also set specific controller/action in the route so that it doens't work for all controllers and/or actions.
I followed the answer to this question but when I apply it I get an error if I try to access views under other controllers.
If I go to http://mydomain/MyActionUnderHome it works fine, but if I go to http://mydomain/SomeOtherController/MyAction it throws
"The resource cannot be found."
Shouldn't the Default route take over if the URL doesn't match the route definition above the Default route?
Are there perhaps new ways in MVC 5 to do this?
My routes:
routes.MapRoute(
"HomeRoute",
"{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"AccountRoute",
"{action}/{id}",
new { controller = "Account", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Better to use Route attribute.
public class HomeController : Controller
{
[Route("")]
public ActionResult Index() { return View(); }
[Route("MyAccount")]
public ActionResult Account() { return View(); }
}
Then add this line t your RouteConfig class before the routes.MapRoute
routes.MapMvcAttributeRoutes();
The issue is that you have is conflicting routes that the router can't resolve correctly
routes.MapRoute(
"HomeRoute",
"{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"AccountRoute",
"{action}/{id}",
new { controller = "Account", action = "Index", id = UrlParameter.Optional }
);
When the router looks at the path /youraction/yourparameter it will resolve to the first route.
The next part of this is that when you provide two values as your URL (as in your example the default router never gets called because the router interprets your input as /action/id rather than controller/action/id
If you change your routes to be:
routes.MapRoute(
"HomeRoute",
"index/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Then you'll get your effect. But your second route can still never be called, as your scheme isn't correct.
I have next map url definition where I want to set specific action and controller for special url:
//localhost:55321/SpecificPart/UserTextId all other routes should work in default way
but based on Route Debugger my URL is mapping to my main route rule how to handle this case ?
routes.MapRoute(
"PartnerSighUp",
"SpecificPart/{id}",
new { controller = "SpecificController ", action = "SpecificAction" },
new { id = "[A-Za-z].+" }
);
routes.MapRoute(
name: "EnglishHomePage",
url: "",
defaults: new { culture = "en", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", culture = "en", id = UrlParameter.Optional }
);
Update if SpecificPart = SpecificController (controller name) it is working but if SpecificPart != SpecificController and looks to other controller it is not working
I have these routes:
routes.MapRoute(
"ActionOnly",
"{action}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { action = "Klub|Historie" });
routes.MapRoute(
"Administrace", // Route name
"Administrace/{controller}/{action}/{id}", // URL with parameters
new { controller = "Administrace", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
// Hrac/Jmeno_hrace
routes.MapRoute(
"Hrac",
"Hrac/{name}",
new { Controller = "Hrac", Action = "Name" }
);
// pro aktivaci uzivatele který se registroval, ale jeste nepotvrdil email
routes.MapRoute(
"Activate",
"Account/Activate/{username}/{key}",
new { controller = "Account", action = "Activate", username = UrlParameter.Optional, key = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Why #Html.ActionLink("Domů", "Index", "Home") is creating website.com/Administrace/Home and not website.com/Home/index and how can I fix it?
Your Administrace route is swallowing all controllers.
You should change it to hard-code the controller name:
routes.MapRoute(
"Administrace", // Route name
"Administrace/Administrace/{action}/{id}", // URL with parameters
new { controller = "Administrace", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
If you want that route to work for multiple controllers, you should replace it with an area (or just add a constraint).
Use #Html.RouteLink instead with the route name being default
Link to something that might help:
What's the difference between RouteLink and ActionLink in ASP.NET MVC?
I could have SWORN I had this working, but somewhere along the line I apparently broke it. Maybe it was during my migration from ASP.NET MVC in VS2008 to ASP.NET MVC2 in VS2010.
My ActionLink:
Html.ActionLink(segment.TitleWithChapterNumber, "Index", "Read", new { bookCode = Model.Product.Id, segmentCode = segment.Index }, null)
The route I expect it to match:
routes.MapRoute(
"Read",
"Read/{bookCode}/{segmentCode}/{sectionCode}/{renderStrategy}",
new { controller = "Read", action = "Index", bookCode = "", segmentCode = "", sectionCode = "", renderStrategy = "" }
);
This renders a link that looks like: http://localhost/Obr/Read?bookCode=14&segmentCode=0
But I want it to look like http://localhost/Obr/Read/14/0
Clicking the link that it renders does take me to the right controller and the response is accurate. If I paste in the link I WANT it to look like, it does work. I guess it's just not matching?
Am I missing something obvious? I've stared at it so long I don't even know what I am looking for anymore.
For reference, here are ALL of my routes:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"ReadImage",
"Read/Image/{bookId}/{imageName}",
new { controller = "Read", action = "Image" }
);
routes.MapRoute(
"Read",
"Read/{bookCode}/{segmentCode}/{sectionCode}/{renderStrategy}",
new { controller = "Read", action = "Index", bookCode = "", segmentCode = "", sectionCode = "", renderStrategy = "" }
);
routes.MapRoute(
"BookReport",
"BookReport/{action}/{folder}",
new { controller = "BookReport", action = "Details", folder = "" }
);
routes.MapRoute(
"Reference",
"Reference/Details/{referenceType}/{searchText}",
new { controller = "Reference", action = "Details", referenceType = "", searchText = "" }
);
routes.MapRoute(
"PaginatedAudits", // Route name
"Audit/Page/{pageNumber}", // URL with parameters
new { controller = "Audit", action = "Index" } // Parameter defaults
);
routes.MapRoute(
"PaginatedReadLog", // Route name
"ReadLog/Page/{pageNumber}", // URL with parameters
new { controller = "ReadLog", action = "Index" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
The action signature looks like this:
[Authorize]
public ActionResult Index(string bookCode, string segmentCode, string sectionCode, string renderStrategy)
{
// code
}
Try a route link by explicitly giving the name of the route:
Html.RouteLink(
segment.TitleWithChapterNumber, // linkText
"Read", // routeName
new {
bookCode = Model.Product.Id,
segmentCode = segment.Index
}, // routeValues
null // htmlAttributes
)
Your route definitions should have UrlParameter.Optional instead of empty strings.
routes.MapRoute(
"Read",
"Read/{bookCode}/{segmentCode}/{sectionCode}/{renderStrategy}",
new { controller = "Read", action = "Index", bookCode = UrlParameter.Optional, segmentCode = UrlParameter.Optional, sectionCode = UrlParameter.Optional, renderStrategy = UrlParameter.Optional }
);
This will also help with redirect in controllers and form urls created through MVC extensions.