ASP.NET MVC Routing & Html.ActionLink - c#

In my routes file I have this setup:
routes.MapRoute(
name: "Stages", // Route name
url: "Stages/{action}", // URL with parameters
defaults: new { controller = "Tasks", action = "Index", taskTypeIds = new[] { TaskTypes.Stage } }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The problem now is that I want to define two main menu items:
<li>#Html.RouteLink("Stages", "Stages")</li>
<li>#Html.ActionLink("Tasks", "Index", "Tasks")</li>
But what this outputs for both is the same URL:
/Stages
Why is it doing this, and can I get around it?

Your controller is Task for stages route.

Related

RouteConfig in ASP.NET MVC C# sent to error page

I'm setting up a new server with MVC 5.2.4.0 in ASP.NET. My problem is with the route configuration.
In the configuration file App_Start/RouteConfig.cs I have put the following code:
routes.MapRoute(
name: "DefaultEvent",
url: "{idseries}/{controller}/{action}/{id}",
defaults: new { idseries= "startgate", idsequel = "SG1", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "DefaultEdition",
url: "{idseries}/{idsequel}/{controller}/{action}/{id}",
defaults: new { idseries= "stargate", idsequel = "SG1", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I want to use this urls which format are:
url:port
url:port/stargate
url:port/stargate/episode/actors
url:port/stargate/episode/actors/ONeill
url:port/stargate/SG1
url:port/stargate/SG1/episode/actors
url:port/stargate/SG1/episode/actors/ONeill
I had understood that ASP.NET tried to find the first MapRoute that does not fail, but it is not like that.
I have used Visual Studio Debugger but this only enters the file RouteConfig.cs at the first execution.
Also, I have created MapRoutes to cover all the combinations but it only enters the first entry. And if it does not enter it fails, showing the 404 error page.
Examples:
routes.MapRoute(
name: "DefaultEvent",
url: "{idseries}/{controller}",
defaults: new { idseries= "startgate", idsequel = "SG1", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "DefaultEdition",
url: "{idseries}/{idsequel}",
defaults: new { idseries= "stargate", idsequel = "SG1", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
With this configuration it fails if I do not put a valid driver.
routes.MapRoute(
name: "DefaultEdition",
url: "{idseries}/{idsequel}",
defaults: new { idseries= "stargate", idsequel = "SG1", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "DefaultEvent",
url: "{idseries}/{controller}",
defaults: new { idseries= "startgate", idsequel = "SG1", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
But if I put it upside down, it counts as the id of the sequel (which I see logic).

Routing to another action than Index as default

I already searched for long, but for this case I found no answer.
I have a HomeController and the default route in my route.config is as follows:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
So; http://localhost:36690/Home automatically calls Index.
I also have a MyController (and it's custom route in route.config) with an Index method and can just write http://localhost:36690/My and it works.
But I want to have a custom route without any Index.
If I request localhost:36690/New, it should call BASIC.
So I tried the following:
routes.MapRoute(
"New",
"{controller}/{action}",
new { controller = "New", action = "Basic" }
);
But it ignores my default action 'Basic' and throws the error:
Server Error in '/' Application. The resource cannot be found. "
You can add another route specifically for that controller.
routes.MapRoute(
name: "New",
url: "New/{action}",
defaults: new { controller = "New", action = "Basic" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
That way, when /New is called it will default to NewController.Basic

Custom map route is not working in asp.net mvc

I have an custom mvc route it's not working. if i define the route before home route then it's working otherwise not.
this code is not working.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Citysearch",
"{state}",
new { controller = "Dashboard", action = "GetDynamicContent" }
);
when i define the citysearch first then it's working something like this
routes.MapRoute(
"Citysearch",
"{state}",
new { controller = "Dashboard", action = "GetDynamicContent" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
and another problem is this is the url of city search http://localhost:51381/dynamic-content. dynamic-content this is my state paramter. It's calling of my Dashboard/GetDynamicContent. but problem is when application run after login url is http://localhost:51381/Home it is calling always Dashboard/GetDynamicContent how to get rid of this problem please help me.
Routes are read from top to bottom. Therefore, the first route match will be used when routing.
Try this
routes.MapRoute(
"Home",
"Home/{action}",
new { controller = "Home", action = "index" }
);
routes.MapRoute(
"Citysearch",
"DynamicContent/{state}",
new { controller = "Dashboard", action = "GetDynamicContent" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
You have to use some fix part in your url as shown below DynamicContent/ to solve issue you are facing. And your url should be like http://localhost:51381/DynamicContent/dynamic-content.
routes.MapRoute(
"Citysearch",
"DynamicContent/{state}",
new { controller = "Dashboard", action = "GetDynamicContent" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I would like to add my experience in this issue "custom-map-route is not working".
Myobservation:custom url routing not working when page is redirected using href="redirected page" . when i am using #Url.Action() for redirection then custom routing started working.
here is my initial code(peice of code)
return '<a href="/Home/AuthorityForm?formid=' + '#ViewBag.formid' + "&SubmittedFormId=" + full.ConsentId + """ + 'target="_blank">ViewForm';
i changed to
var link = '#Url.Action("AuthorityForm", "Home", new { formid = ViewBag.formid, SubmittedFormId = "-1" })';
link = link.replace("-1", full.ConsentId);
return 'ViewForm'
this may help someone..

Url.Action() does not remove default values

I have rather a simple routing map.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{seoName}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, seoName = UrlParameter.Optional }
);
Now, If use Url.Action("Index", "Home"), it does not properly remove defaults values of the route. And it gives me /Home/Index.
Now, if I remove either {id} or {seoName}, and its corresponding default value, then the URL is properly generated like / (root).
What I am missing here? It does not seem to be an ambient value, since I am visiting the main page with no ids, nor seoNames.
Any ideas?
You would need multiple mappings to achieve what you want as you are only allowed to make the last route placeholder optional.
routes.MapRoute(
name: "SeoFriendly",
url: "{controller}/{action}/{id}/{seoName}",
defaults: new { controller = "Home", action = "Index", seoName = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

C# MVC Route detection

I need to call customer profile page like "(www.mysite.com/John) or (www.mysite.com/customer name)"
so i had add route to be like that
routes.MapRoute(
name: "Profile",
url: "{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
but it always go to the first route as if i need to open any controller it does n`t work
any advice?
Thanks.
the route wont work like that so you have two options
1) When going to any action your URL should be www.mysite.com/controller/action or else it will give HTTP 404 error
2) You can make route unique by adding prefix www.mysite.com/Customer/John like this
routes.MapRoute(
name: "Profile",
url: "Customer/{id}",
defaults: new { controller = "Home", action = "Index"}
);
and your action should be
public ActionResult Index(string id)
{
string SurveyName = "";
if (id != null)
SurveyName = id;
if (!string.IsNullOrEmpty(SurveyName))
{
ViewBag.Survey = SurveyName;
}
return View();
}
You need to change your controller and action according to your requirement, you are still providing default values to controller and action.
You can also use Html.ActionLinkat to refer to your desired view
ex. Html.ActionLink("action_name","Controller_name")
Your route has a single optional parameter and is working like a catch all route.
[EDIT]
You can't use the constraint as I said on my previous answer if you don't have an pattern to restrict the route. Based on this you can remove the id = UrlParameter.Optional from defaults (by this change your route will be reached only when the url contains the id parameter).
routes.MapRoute(
name: "Profile",
url: "{id}",
defaults: new { controller = "Employee", action = "Index"}
);
Another option is to create a custom route constraint to check if your profile id is valid but take care since it can slow down your application since it'll be executed on every request (maybe caching your profile list could help on this).
[OLD ANSWER]
An easy way to restrict a route scope is using constraint.
The route below is reached just if the id parameter is a value with 4 digits (you can use your own regex to fit your requirements).
routes.MapRoute(
name: "Profile",
url: "{id}",
defaults: new { controller = "Employee", action = "Index"},
constraints: new { id = #"\d{4}" }
);
Make these changes. It worked for me.
routes.MapRoute(
name: "Profile",
url: "{id}",
defaults: new { controller = "NewCon", action = "Demo", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Categories