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

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).

Related

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

ASP.NET MVC Routing & Html.ActionLink

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.

How many routing parameters can you have?

How many routing parameters can you have in .NET MVC?
Like localhost:port/controller/action/parameter1/parameter2/parameter3/ and so on
I'm currently using this for my routing
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{extra}/{extra2}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, extra = UrlParameter.Optional, extra2 = UrlParameter.Optional }
);
And this works great.
But when I add yet another routing like an "extra3" in this example the whole page just breaks, ordinary form posts and so on stops working.
Is there a limit to how many you can have and should I rather just make normal querystrings?
Edited
To explain it further.
I want to do this
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{extra}/{extra2}/{extra3}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, extra = UrlParameter.Optional, extra2 = UrlParameter.Optional, extra3 = UrlParameter.Optional }
);
But it just simply does not work when I add even further amount of routes/parameters.
Is there a limit to how many you can have?
HTML Helpers e.g. (Html.ActionLink, Html.Action, Html.BeginForm etc) gives wrong results when there are more optional parameters in routs. Normally we keep one optional parameter at the end of the route as optional.
If you keep the routing config as follows it will work.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{extra}/{extra2}/{extra3}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, extra = UrlParameter.Optional, extra2 = UrlParameter.Optional, extra3 = UrlParameter.Optional }
);
routes.MapRoute(
name: "OneOptional",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Read the section "The Root Cause" of following post understand the issue with multiple optional parameters.
http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx/
Thanks!

ASP.Net MVC 5 routing with culture url

I followed this guide and it works fine with my language support. Url's such as
http://domain.com:33982/en-us/Test works great.
However my issue is that I want it to work with
http://domain.com:33982/Test as well. I can't figure it out how to route it without the culture in the link...
My routes in the RouteConfig.cs
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "MissingCulture",
url: "{controller}/{action}/{id}",
defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "Test", action = "PageMissing", id = UrlParameter.Optional }
);
I don't really understand why it doesn't work. When I go to /Test to just redirects me to the default Home/Index.
Any suggestions? Thanks
That is because the first and second route both match. The optional parameters cause the application to choose the first one that matches: so the first one always gets picked.
Try this:
routes.MapRoute( name: "DefaultWithCulture"
, url: "{culture}/{controller}/{action}/{id}"
, defaults: new { culture = "en-us", controller = "Home", action = "Index", id = UrlParameter.Optional }
, constraints: new { culture = "[a-z]{2}-[a-z]{2}" }
);
routes.MapRoute( name: "Default"
, url: "{controller}/{action}/{id}"
, defaults: new { culture = "xx-xx", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
It works over here for (retulting culture behind them):
http://host/ (en-us)
http://host/Home/Index (xx-xx)
http://host/zh-cn/Home/Index (zh-cn)

Categories