I have routes below in my MVC4...
routes.MapRoute("Account", "Account/{action}", new { controller = "Account", action = "Index" });
routes.MapRoute("Admin", "Admin/{action}", new { controller = "Admin", action = "Index" });
routes.MapRoute("Custom", "{action}", new { controller = "Home", action = "Index" });
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
...and need to find a way to route all url which has 2 symbol(www.domain.com/word2pdf) in url to action Index and pass url path(word2pdf) as api parameter.
public ActionResult Index(string api)
{
}
Any ideas how to do that?
As #Murali said, the proper way is to use Attribute Routing, but that's a new feature for mvc5 only.
If you want or have to stay on mvc4:
routes.MapRoute(name: "SomeRoutingName",
url: "{api}",
defaults: new { controller = "SomeControllerName", action = "Index"},
constraints: new { api= ".+2.+"});
Also just for some lol's, this should also work:
routes.MapRoute(name: "SomeRoutingName",
url: "{partA}2{partB}",
defaults: new { controller = "SomeControllerName", action = "Index" });
In Controller:
public ActionResult Index(string partA, string partB)
{
var api = string.Concat(partA,"2",partB);
}
Related
I have next specific map routes
routes.MapRoute(
"MyPagePost",
"URL-Up/{name}",
new { controller = "MyController", action = "MyPostAction" },
new { httpMethod = new HttpMethodConstraint("POST") }
);
routes.MapRoute(
"MyPageGet",
"URL-Up/{name}",
new { controller = "MyController", action = "MyGetAction" },
new { name = "[A-Za-z].+", httpMethod = new HttpMethodConstraint("GET") }
);
my default controller looks like
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", culture = "en", id = UrlParameter.Optional },
constraints: new { culture = #"[a-zA-Z]{2}" }
);
and the issue is next:
my get MyPageGet route show a page with include FORM with POST reqvest to MyPagePost route, but on a first call I am getting the same GET request and see in URL other extra param ?culture=de. Moreover, with or without this parameter, second call it working fine via MyPagePost route.
UPDATE:
In Chrome or fiddler Logs I see that reqvest to URL-Up/Bla-Bla has 302 status and response heared is URL-Up/Bla-Bla?culture=de. Why it can't be processed ?
just try it with
#using(Html.BeginRouteForm("MyPagePost",FormMethod.Post))
{
<input type="submit" value="Submit"/>
}
The routes in your post working for me in both html.beginform and html.beginrouteform on the first time.
i try it with the following routes and action methods
routes.MapRoute(
"MyPagePost",
"URL-Up/{name}",
new { controller = "Home", action = "PostAction" },
new { name="[A-Za-z].+", httpMethod = new HttpMethodConstraint("POST") }
);
routes.MapRoute(
"MyPageGet",
"URL-Up/{name}",
new { controller = "Home", action = "GetAction" },
new { name = "[A-Za-z].+", httpMethod = new HttpMethodConstraint("GET") }
);
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", culture = "en", id = UrlParameter.Optional },
constraints: new { culture = #"[a-zA-Z]{2}" }
);
public ActionResult GetAction()
{
return View();
}
[HttpPost]
public ActionResult PostAction()
{
return View();
}
I have the following URl:
http://localhost:12981/BaseEvent/EventOverview/12?type=Film
This is route:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I want that in a browser the url looks like:
http://localhost:12981/Film/Overview/12
How can I do this?
One more example:
http://localhost:12981/BaseEvent/EventOverview/15?type=Sport
should be
http://localhost:12981/Sport/Overview/15
Thanks.
This should work:
routes.MapRoute("", "{type}/Overview/{id}", new { controller = "Events", action = "Overview");
Then you have a controller named EventsController with an action like this one
public ViewResult Overview(string type, int id)
{
//Your code
return View(model);
}
I have this URL:
/controller/action/value
and this action:
public ActionResult Get(string configName,string addParams)
{
}
How do I set up my routing table to get the routing engine bind the value to the configName parameter for any action in the Config controller?
Well, first off, that is incomplete. You don't have a method name.
Secondly, this will already work with URLs of the format:
/controller/action?configName=foo&addparams=bar
Here's how to do it with pretty routes:
routes.MapRoute(
"YourMapping",
"{controller}/{action}/{configName}/{addParams}");
or
routes.MapRoute(
"YourMapping",
"{controller}/{configName}/{addParams}",
new {
controller = "YourController",
action = "YourAction"
},
new {
controller = "YourController" // Constraint
});
if you want to exclude the action from the URL.
You could add a new route above the default
routes.MapRoute(
"Config",
"config/{action}/{configName}/{addParams}",
new { controller = "Config", action = "Index" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Which will allow you to use the route /config/actionName/configName/addParamsValue. Your other routes should be unaffected by this.
routes.MapRoute(
"ValueMapping",
"config/{action}/{configName}/{addParams}",
new { controller = "Config", action = "Index", configName= UrlParameter.Optional, addParams = UrlParameter.Optional } // Parameter defaults);
Setting default Controller to Home, with a Default Action of Index
So the Url:
/config/get/configNameValue/AddParamValue
would match this Method:
public ActionResult Get(string configName,string addParams)
{
//Do Stuff
}
I have an ASP.Net MVC 3 application. With 2 Areas:
Auth - handles all the authentication etc
Management - area for property management
In the Management Area I have a ManagementController and a PropertyController. The ManagementController does nothing, it only has a simple Index ActionResult than return a view.
The PropertyController has an Index ActionResult that returns a view with a list of properties as well as an Edit ActionResult that takes a propertyId as parameter. In the Property Index view i have a grid with the list of properties and an option to edit the property with the following code:
#Html.ActionLink("Edit", "Edit","Property", new { id = item.PropertyId })
In theory this should redirect to the Edit ActionResult of my Property Controller,however it redirects to the Index ActionResult of my ManagementController. My ManagementAreaRegistration file looks like this:
context.MapRoute(null, "management", new { controller = "management", action = "Index" });
context.MapRoute(null, "management/properties", new { controller = "Property", action = "Index" });
context.MapRoute(null, "management/properties/Edit/{propertyId}", new { controller = "Property", action = "Edit" });
And my global.asax's RegisterRoutes like this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
What am I missing, why would it redirect to the wrong controller and action?
Thanks in Advance!
You might need to specify the area in your route values parameter:
#Html.ActionLink("Edit", "Edit", "Property", new { id = item.PropertyID, area = "Management" }, new { })
Based on the constructor used for this call, you need to specify the last parameter, htmlAttributes, which, for your purposes, would be empty.
In your route you defined a parameter called propertyId not id:
context.MapRoute(null, "management/properties/Edit/{propertyId}", new { controller = "Property", action = "Edit" });
Try this:
#Html.ActionLink("Edit", "Edit","Property", new { propertyId = item.PropertyId })
I'd suggest using constraints.
For example my default route in the Global.asax is as follows:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { typeof(MyProject.Controllers.HomeController).Namespace });
Then in my area registration:
context.MapRoute(
"Search_default",
"Search/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { typeof(MyProject.Areas.Search.Controllers.HomeController).Namespace }
);
This means I get {AppName}/ as well as {AppName}/Search/Home and both work a treat.
From any Area to Home do following
Url.Action("Details", "User", new { #id = entityId, area = "" })
From Home to any Area
Url.Action("Details", "Order", new { #id = entityId, area = "Admin" })
How do I make my application route to mydomainname/username/controller.
I am working on asp.net mvc web application that enables a user to belong to multiple account. ie. In the application every account has its own users, and each user in one account can also be a user in another account. What i need is when a user wants to login, they specify the account they want to be logged to like this: domainname.com/accountname/login.
Am able to do this, but where am having issue is how to persist the accountname route parameter across other routes? I mean making it to be visible on the url. For now am using cookie to store and get the accountname parameter, but i need a way to make it visible on the url in every request (without having to manually route it on links) until the user singout.
Am using asp.net mvc 2
Edit: Added my route code
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("", "", new { controller = "Home", action = "index" });
routes.MapRoute("", "dashboard", new { controller = "account", action = "dashboard" });
routes.MapRoute("", "contacts", new { controller = "contact", action = "index" });
routes.MapRoute("", "groups", new { controller = "group", action = "index" });
routes.MapRoute("", "sms", new { controller = "sms", action = "index" });
routes.MapRoute("", "users", new { controller = "user", action = "index" });
routes.MapRoute("", "login", new { controller = "Home", action = "login", accountUrlName = UrlParameter.Optional });
routes.MapRoute("", "{accountUrlName}/login", new { controller = "Home", action = "login" });
routes.MapRoute("", "register", new { controller = "home", action = "register" });
routes.MapRoute("", "{accountUrlName}/invitations/{ivkey}", new { controller = "home", action = "invitations" });
routes.MapRoute("", "{urlName}",
new { controller = "home", action = "index", urlName = UrlParameter.Optional });
routes.MapRoute("", "{accountUrlName}/{action}",
new { controller = "account", action = "dashboard", id = "", accountUrlName = UrlParameter.Optional });
routes.MapRoute("", "{accountUrlName}/{controller}/{action}/{id}",
new { controller = "account", action = "dashboard", id = "", accountUrlName = ""});
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
So you effectively have two sets of routes here -
{AccountName}/{Controller}/{Action} and {Username}/{Controller}/{Action}.
Is this right?
It's possible for you to create these routes, but you'd have to have usernames which do not contain account names, and vice versa.