I have a MainController with an Index action. I want to pass in Index action the last fragment of request url. I have tried this route:
routes.MapRoute(
"Main",
"Main/#!/{alias}",
new { controller = "Main", action = "Index" }
);
with this Index action:
public ActionResult Index(string alias)
{
// code
}
but this is not working. Where is the mistake?
This way
routes.MapRoute(
"Main",
"Main/#!/{alias}",
new { controller = "Main", action = "Index", alias= UrlParameter.Optional }
);
Related
How do I map unmatched routes to the index action for that controller?
I'm using a client side router for routes like /Home/foo
routes.MapRoute(
name: "Test",
url: "{controller}/{*.}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
This currently results in a 404.
Your route that you used is correct, the problem is the orders of the routes that need to be added in write format:
for example if you have some routes like:
routes.MapRoute(
name: "PreTest",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Test",
url: "{controller}/{*.}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
So it is always matched with first route PreTest. Check your routes order. It is work like a dictionary that ordered. Check this for more information.
I would create an AuthorizeAttribute to handle your case. Then you can decorate your controller with that attribute.
Here's a small example to redirect your action base on a value in the route:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class RedirectAttribute:ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if(filterContext.Controller.ControllerContext.RouteData.Values.ContainsValue("Foo"))
{
//Redirect to the login for example
UrlHelper urlHelper = new UrlHelper(filterContext.HttpContext.Request.RequestContext);
string url = urlHelper.Action("actionName", "controllerName");
filterContext.Result = new RedirectResult(redirectUrl);
}
}
}
Here's how to use it in a controller:
[Redirect]
public class MyCustomController : AsyncController
{
public ActionResult Index()
{
return View();
}
public ActionResult Foo()
{
//It will redirect
return View();
}
}
I need to define an MVC route for URL like this:
http://localhost/RealSuiteApps/RealHelp/-1/Detail/BRK18482020
where:
Detail - is controller name
default action Index should be executed
-1 is some client id
BRK18482020 is orderId
I need this to go to DetailController, Index action with orderId parameter.
I tried this:
routes.MapRoute(
name: "Detail",
url: "Detail/{id}",
defaults: new { clientid = "-1", controller = "Detail", action = "Index", id = UrlParameter.Optional }
);
but I get a message "Page Not Found". What am I missing here ??
Assuming DetailController action
public ActionResult Index(int clientId, string orderId) { ... }
Then route would be mapped as
routes.MapRoute(
name: "Detail",
url: "{cientId}/Detail/{orderId}",
defaults: new { clientid = "-1", controller = "Detail", action = "Index" }
);
Note that this should also be registered before any default routes.
I have the following action result:
public ActionResult Index(int id, int? siteId)
{
//code here....
return View(object);
}
I have the following route mapping as follows:
routes.MapRoute(
name: "SettingsRoute",
url: "Settings/{id}/{siteId}",
defaults: new
{
controller = "Settings",
action = "Index",
}
);
What do I need to do, so the url will be "Settings?id=1&siteId=133" instead of the format "Settings?id=1" durring initial load. Then to select a site it builds the URl "Settings/1/133".
I am using the following actionLink to create this:
<li>#Html.ActionLink(site.Name, "Index", "Settings", new { id = Model.SettingsEnvironment.EnvironmentID, siteId = site.SiteID }, null)</li>
I can't seem to get the routing down right. Any help would be appreciated. Thanks.
You need to set your optional URL parameter:
routes.MapRoute(
name: "SettingsRoute",
url: "Settings/{id}/{siteId}",
defaults: new
{
controller = "Settings",
action = "Index",
siteId = UrlParameter.Optional
}
);
ref: http://haacked.com/archive/2010/02/12/asp-net-mvc-2-optional-url-parameters.aspx/
working on an MVC project and I'm having a tough time with rerouting my URL.
What I currently have is
http://dev.mywebsite.com/s/index?Key=abc123
which then runs the index action and completes as I'd like it to
I'd like to be able to type in
http://dev.mywebsite.com/s/abc123
and run the index action like normally
I currently have
routes.MapRoute(null, "s/index/{id}", new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
but I'm kind of stuck as to where to go from here. Any assistance would be greatly appreciated.
Thanks
Edit: My full routeconfig class
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("sites/{*pathInfo}");
routes.MapRoute(null, "s/index/{key}", new
{
controller = "S",
action = "Index",
key = UrlParameter.Optional
}
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Site", action = "Index", id = UrlParameter.Optional },
new[] { "Custom.Web.Controllers" }
);
}
in my controller I have the actionresult index as
public ActionResult Index(string Key)
{
return Redirect(workflow.RetrieveURL(Key));
}
So, after all our comments, the solution is:
routes.MapRoute(null, "s/{Key}",
new {
controller = "Home",
action = "Index",
Key = UrlParameter.Optional
});
Place this rule before all the others to give it preference
EDIT: Sorry I explained it badly. Basically, in the below example, I want "this-is-handled-by-content-controller" to be the "id", so I can grab it in ContentController as an action parameter, but I want to access it via the root of the site, e.g mysite.com/this-is-not-passed-to-homecontroller.
I'm trying to create a root route that will go to a separate controller (instead of home).
I've followed the "RootController" example posted somewhere else that implements IRouteConstraint but it just doesn't seem to work and I've already wasted a couple of hours on this!
Basically, I have a LoginController, a HomeController, and a ContentController.
I want to be able to view HomeController/Index by going to http://mysite/. I want to be able to view LoginController/Index by going to http://mysite/Login. But.. I want the ContentController/Index to be called if any other result occurs, e.g: http:/mysite/this-is-handled-by-content-controller
Is there an elegant way to do this that works?
This was my last attempt.. I've cut/pasted/copied/scratched my head so many times its a bit messy:
routes.MapRoute(
"ContentPages",
"{action}",
new { Area = "", controller = "ContentPages", action = "View", id = UrlParameter.Optional },
new RootActionConstraint()
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { Area = "", controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "Website.Controllers" }
);
Any help is appreciated greatly!
chem
I would do something similar to this, though that might not be the best solution if you keep adding more controller in the future.
routes.MapRoute(
"HomePage",
"",
new { controller = "Home", action = "Index", id="" }
);
routes.MapRoute(
"Home",
"home/{action}/{id}",
new { controller = "Home", action = "Index", id="" }
);
routes.MapRoute(
"Login",
"Login/{action}/{id}",
new { controller = "Login", action = "Index", id="" }
);
//... if you have other controller, specify the name here
routes.MapRoute(
"Content",
"{*id}",
new { controller = "Content", action = "Index", id="" }
);
The first route is for your youwebsite.com/ that call your Home->Index. The second route is for other actions on your Home Controller (yourwebsite.com/home/ACTION).
The 3rd is for your LoginController (yourwebsite.com/login/ACTION).
And the last one is for your Content->Index (yourwebsite.com/anything-that-goes-here).
public ActionResult Index(string id)
{
// id is "anything-that-goes-here
}
Assuming you have ContentController.Index(string id) to handle routes matching the constraint, this should work:
routes.MapRoute(
"ContentPages",
"{id}",
new { Area = "", controller = "Content", action = "Index" },
new { id = new RootActionConstraint() }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { Area = "", controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "Website.Controllers" }
);