MVC Reroute to accept Key - c#

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

Related

ASP.NET MVC C# Multiple Url Routes Config Controller Action

I want create route config multiple Url. ex: "fr-ca/user/login" and "en-ca/user/login" and its working, but if click submit and proccess data by controller and return RedirectToAction(MVC.User.Dashboard); always return "fr-ca/user/dashboard";
Although I use Url "en-ca/", and always return all link MVC.anything in first is "fr-ca/"
Because in position route config, "fr-ca" is first.
Maybe somebody can help me to solve this, thank u.
routes.MapRoute(
name: "fr-Default",
url: "fr-ca/{controller}/{action}/{id}",
defaults: new { controller = MVC.Content.Name, action = MVC.Content.ActionNames.Show, id = UrlParameter.Optional, siteId = Site.FR.Id },
namespaces: new string[] { "Jay.Cms.Web.Controllers" }
);
routes.MapRoute(
name: "en-Default",
url: "en-ca/{controller}/{action}/{id}",
defaults: new { controller = MVC.Content.Name, action = MVC.Content.ActionNames.Show, id = UrlParameter.Optional, siteId = Site.EN.Id },
namespaces: new string[] { "Jay.Cms.Web.Controllers" }
);
You can write to action attribute in controller
'[Route("fr-ca/{controller}/{action}/{id}")]
[Route("en-ca/{controller}/{action}/{id}")]
public ActionResult Home()
{
return View();
}'

Creating custom route in ASP NET

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 }
);

MVC routing only applying to certain action results

Do I have to route a special route for every action result in a controller, or do you do one route, and have to live by that standard thought the controller? I thought you could make a default route, and then a special route for any instance you wanted. I keep running into a problem where one of my routes will hit my action Results correctly, but then the others no longer work. This code is probably the wrong way, but hence why I am posting it here. PLease try to clarify this for me if you can. I understand that I am suppose to be able to do {controller}/{action}/{id} for example. So that should hit Settings/GetSite/{siteid} for the following
public ActionResult GetSite(int id);
Routes configuration:
routes.MapRoute(
"SettingsUpdateEnviorment",
"{controller}/{action}",
new { controller = "Settings", action = "UpdateProperties" },
new { httpMethod = new HttpMethodConstraint("POST") }
);
routes.MapRoute(
name: "ProfileRoute",
url: "Profiles/{userId}",
defaults: new
{
controller = "Profile",
action = "Index",
}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Settings", // Route name
"Settings/{id}", // URL with parameters
new { controller = "Settings", action = "Index" } // Parameter defaults
);
Controller Code:
public ActionResult Index(int id)
{
return View(model);
}
public ActionResult GetSite(int enviornmentID, string name)
{
return RedirectToAction("Index");
}
[HttpPost]
public ActionResult AddSite(int id)
{
return RedirectToAction("Index", new { id = id });
}
So, the URL works as expected for Settings/1 to hit the Index actionresult Index(int id). Then, when I try to do the ActionResult for GetSite(int enviornmentID, string name) using the following actionLink:
#Html.ActionLink(site.Name, "GetSite", "Settings", new { enviornmentID = Model.Enviorment.EnvironmentID, name = site.Name }, null)
It creates the URL correctly as follows: Settings/GetSite?enviornmentID=1&name=CaseyTesting2, but gives me an error stating that I am trying to send a null value to my Index(int id) actionResult. I thought that since I am using the action name and it's same params, that MVC will figure the route out? Why is this not functioning for me, or what I am doing wrong? Thanks!
I realized what I was doing thanks to this article http://www.itworld.com/development/379646/aspnet-mvc-5-brings-attribute-based-routing. I was mixing up the order, when I had everything else correct. Then I was missing the param names being identical, when everything else was correct. So I kept having minor issues when trying to find the problem out. I also switched to MVC5's attribute routing, and like it much more.
So this is my code that is now working:
RoutConfig
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "ProfileRoute",
url: "Profiles/{userId}",
defaults: new
{
controller = "Profile",
action = "Index",
}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The controller code
[Authorize]
[RoutePrefix("settings")]
[Route("{action=index}")]
public class SettingsController : ZenController
{
[Route("{id:int}")]
public ActionResult Index(int id)
{
return View(model);
}
[Route("GetSite/{sitename:alpha}")]
public ActionResult GetSite(string sitename)
{
return RedirectToAction("Index");
}
Thanks again everyone! Happy coding!

Trying to format URL for Action using multiple params in MVC

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/

C# MVC 3 Root Route doesn't seem to work

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" }
);

Categories

Resources