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..
Related
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 }
);
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 }
);
I'm trying to setup a route that's routes to a div on a page in my RouteConfig.cs file but doesn't work (HTTP Error 403.14 - Forbidden).
routes.MapRoute(
name: "Default",
url: "{controller}/{action}#{locId}/{id}",
defaults: new { controller = "Home", action = "Index", locId =
"theDiv", id = UrlParameter.Optional }
);
Can somebody help me out please?
Thanks in advance.
This should do it,
routes.MapRoute(
name: "WithId",
url: "{controller}/{action}/{id}#{HTMLElementId}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, HTMLElementId = UrlParameter.Optional }
);
this sets it as an optional parameter.
thanks
Try to build your route differently using this format :
url:"{controller}/{action}/{id}#{locid}",
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.
I have a next routes settings:
routes.MapRoute(
name: "TestCC",
url: "TestCC/{action}",
defaults: new { controller = "PaymentHub", action = "Cancelled" }
);
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
When user is open TestCC action I try to redirect them to other action that should conteine {culture}
public ActionResult TestAction()
{
TempData["pp"] = Request["p1"];
TempData["dd"] = Request["p2"] ;
return RedirectToAction("OtherAction", "OtherController");
}
How to add {culture} part to redirect in RedirectToAction?
I don't want to write culture="value" to Default route.
Not sure if I understand you correctly, but I guess this will do what you want:
RedirectToAction("OtherAction", "OtherController", new {culture = "value"});