asp.net-mvc-routing

MVC 4: Custom Route

老子叫甜甜 提交于 2019-12-11 00:38:46
问题 ASP.NET MVC 4 website. Got a database-table named "Locations", which holds only three possible Locations (eg. "CA","NY","AT") The default route would be: http://server/Location/ --- list of Locations http://server/Location/NY --- details of NY-Location How can I create a custom route without the /Location/ - bit? (which I find a bit more nice) So that http://server/NY - details of NY http://server/AT - details of AT .... etc... and http://server/Location --- list of Locations 回答1: A solution

ASP NET attribute routing array

我与影子孤独终老i 提交于 2019-12-11 00:35:26
问题 So I am using attribute routing for my controller like this: [Route("last/{id:int}")] public IHttpActionResult GetUpdateLast([FromUri] int id) { return Ok(); } Now I want to convert it to accept an array of integers, in the controller parameter I just switch to [FromUri] int id[] with no problems, however, how do I switch the route attribute [Route("last/{id:int}")] to make it accept an array of integers? 回答1: Your were almoust there, there is no way of doing what you want using the route, so

asp.net mvc Find Controller By Name With Area

三世轮回 提交于 2019-12-10 22:12:52
问题 My goal is to find a controller from it's name and area. I have successfully done this if my current httpContext is within the same Area as the to-be-found controller. However, I cannot get my call to the ControllerFactory to take Area into consideration. Here's my code: public static ControllerBase GetControllerByName(this HtmlHelper htmlHelper, string controllerName) { IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory(); IController controller = factory

ASP.NET MVC Custom Route class - Can't get route data

允我心安 提交于 2019-12-10 21:13:59
问题 I'm trying to implement my own route class, inheriting from the default Route. This is what my custom route class looks like: public class FriendlyRoute : Route { public FriendlyRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler) : base(url, defaults, routeHandler) { } public override RouteData GetRouteData(HttpContextBase httpContext) { var routeData = base.GetRouteData(httpContext); var controllerName = routeData.Values["controller"].ToString(); var actionName =

%20 followed by slash followed by more data causes ASP.NET MVC 3 routing to fail?

孤者浪人 提交于 2019-12-10 21:09:49
问题 When the characters %20 appears in between paramaters a url, my MVC routing stops considering that a string. Why is that, and how can I approach handling "%20" characters in my URL? Example URL http://localhost:40494/ListContents/Delete/asdf%20/5430f394... public ActionResult Delete(string DNSName, Guid id) {...} routes.MapRoute( "Delete", // Route name "ListContents/Delete/{DNSName}/{id}", // URL with parameters new { controller = "ListContents", action = "Delete" } // Parameter defaults );

MVC routing with one fixed action and controllers with multiple optional parameters

让人想犯罪 __ 提交于 2019-12-10 20:29:56
问题 basically i have a problem, i want to make one default action in multiple controllers and use multiple optional parameters with my custom url as below: www.mydomain.com/{controller name}/{v1}/{v2}/{v3}/{v4} and also do not want action name in url. I have this routing in routeconfig.cs routes.MapRoute( name: "Blog", url: "{controller}/{v1}/{v2}/{v3}/{v4}", defaults: new { controller = "Blog", action = "searchBlog", v1 = UrlParameter.Optional, v2 = UrlParameter.Optional, v3 = UrlParameter

How to prevent certain internal routes from being accessed by the user?

☆樱花仙子☆ 提交于 2019-12-10 19:55:55
问题 I have a site with a lot of routes. Some routes, e.g. /sector-overview are to a specific page that I want the user to see. Other routes, e.g. /sectoroverview are to an an action that ultimately renders a partial which is included on the homepage. the second route is only meant to be internal to the application, but if the user types that into their address bar (it's an easy mistake to make), the system sees that as a valid request and it'll return the HTML partial. I could rename the second

Create sub folders in the controller without Areas

隐身守侯 提交于 2019-12-10 19:47:44
问题 I am developing a MVC 3 Web app and I want to create something like this: /Controller /Blog BogController.cs ViewsController.cs ArticlesController.cs /Customers SalesController.cs ProductsController.cs HomeController.cs /Views /Blog Index.aspx Summary.aspx /Views Index.aspx Admin.aspx Show.aspx /Articles Show.aspx Admin.aspx /Customers /Sales Index.aspx Totals.aspx /Products Index.aspx Promotions.aspx /Home Index.aspx Create sub folders in the controller But in the solution that they answer

Default area view cannot be found when using MVC 5 RouteArea attribute

梦想与她 提交于 2019-12-10 19:40:59
问题 I have an MVC5 project with multiple areas. I have a default area (named Default ) and within it, a default controller (named DefaultController ). This is accessible at site route. [RouteArea] public class DefaultController : Controller { [Route] public ActionResult Index() { return View("Index"); } } public static void RegisterRoutes(RouteCollection routes) { routes.LowercaseUrls = true; routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapMvcAttributeRoutes(); routes.MapRoute( name:

Prevent spaces in URL showed like %20

坚强是说给别人听的谎言 提交于 2019-12-10 19:00:53
问题 I would like to display nice URL in address bar and avoid spaces to display like %20 Here is an example: Is it possible to replace these spaces with a dash ? Something like: /BANQUE/International-Ledger Maybe something to do in the routing ? Thanks. 回答1: You dont want to replace spaces in generated route, you want to not generate them in the first place. What is your "International Ledger" ? If it is action, then use [ActionName("International-Ledger")] If it is some kind of product or