Get url for custom route in asp.net mvc - asp.net-mvc

In my ASP.Net mvc application I have changed the default route registration in startup.cs to
routes.MapRoute(
name: "default",
template: "{customer}/{language=fi-FI}/{controller=Home}/{action=Index}/{id?}");
So all routes would contain the customer id and the current language. Custom segment matching takes care of putting the customer and language info in new urls so when the requested url is /customer1/fi-fi/somecontroller/someaction any generated url like this
<a asp-controller="othercontroller" asp-action="otheraction">Some action</a>
would be generated correctly with the customer and language code in it.
The question is, how should I generate the url so I can specify the customer and language code without having to do string concatenation? I would need this e.g. in the links for changing language.
I have tried
<a asp-controller="somecontroller" asp-action="someaction" asp-route="default" asp-route-customer="customer2" asp-route-language="en-us">Some action in other language and other customer</a>
but that says that I cannot specify action and controller when asp-route is defined and if I remote asp-route then nothing changes.

I got this working by using #Url.RouteUrl, e.g.
#Url.RouteUrl("default", new { customer = "someothercustomer", language = "someotherlanguage", controller = "somecontroller", action = "someaction" })

Related

Url problems in MVC C#

I have a problem in MVC project in url.
In the Route config I have the following code:
routes.MapRoute(
name: "Test",
url: "{controller}/{action}/{id}/{selected}/{category}/{engineId}‌​",
defaults: new { controller = "Product", action = "SubCategories"}
);
The parameter category contains the name of selected category. In database I have the category with name: "Packet / Set". If in my website I choose this category and in the url will appear this categoryname
SubCategories/92/Bertone-FREECLIMBER-2.0/Packet / Set /33720‌%E2%80%8B.
I got the
Server Error in '/' Application.
The resource cannot be found.
error.
If the the category name doesn't contain the "/" character, evrything works fine:
SubCategories/94/Bertone-FREECLIMBER-2.0/Air%20filter/33720%E2%80%8C%E2%80%8B
Could you advise how should I resolve this?
You need to encode the all slashes in URL as %2F. You can use Javascript encodeURIComponent() function. MVC automatically decodes URL and action parameters so you should be able to get values correctly in your controller without any additional effort.
Take a look here for more info: https://www.w3schools.com/jsref/jsref_encodeuricomponent.asp

Changing URL without changing Actual Path to Redirect

I am new to ASP.Net and working on MVC 4. I want to replace my current URL with a customized URL.
For example:
Current URL: http://www.testsite.com/home?pageId=1002
Desired URL: http://www.testsite.com/1002/home/
So the URL that is displayed in the address bar will be the desired one and actual URL working will be the current one.
I have tried URL routing in Global.asax file of my project but doesn't seems to be working for me.
What exactly I want is to put the URL Like this.
Thanks in Advance.
ASP.NET MVC 4 provide a toolbox way to write your application. The URL that you see in the browser comes from Routing that do the hard work to convert url to app routes and app routes to url.
1) The default ASP.NET MVC 4 Template project comes with a file at App_Start folder named RouteConfig, where you must config the routes for the app.
2) The routes has precedence order, so, put this route before the default one:
routes.MapRoute(
name: "RouteForPageId",
url: "{pageId}/{action}",
//controller = "Home" and action = "Index" are the default value,
//change for the Controller and action that you have
//pageId is the parameter from the action that will return the page
defaults: new { controller = "Home", action = "Index" }
);
Now you can enter myappdomain/1220/index for exemple.
Hopes this help you! Take a look here for more info ASP.NET Routing!

Outgoing Routing in asp.net mvc

I'm reading Pro ASP.NET MVC 3.0 from Appress.Chapter 11 is about URLs routing system.
In passing extra variables from outgoing section is explained about getting segment values from url
for example for bellowing routing :
routes.MapRoute("MyRoute", "{controller}/{action}/{color}/{page}");
if a user is currently at the URL /Catalog/List/Purple/123, and we render a link as
follows:
#Html.ActionLink("Click me", "List", "Catalog", new {page=789}, null)
The routing system will match against the route and It will generate the
following HTML:
Click me
But when i use this code for the following example it generates:
Click me
i don't understand why?
Try moving that route mapping above the other route mappings in your global.asax file

Troubles creating URLs in a VB.NET MVC view

I'm trying to write out animals from a database. All the samples I see for creating View pages have hard-coded "nodes" in the URLs. I can get my first view to work but my second one doesn't write out the correct URL to go to the third one. So, for example, I want my URLs to be:
/animals/
/animals/canines/
/animals/canines/schnauzer
I have the default route setup:
> routes.MapRoute( _
> "Default", _
> "{controller}/{action}/{id}", _
> New With {.controller = "Home", .action = "Index", .id =
> UrlParameter.Optional} _
> )
The first URL works great and I can list the animal groups on the first one along with the correct links to the second URLs. I do that as follows:
for each animal in model
Html.ActionLink(animal.animal_name,animal.animal_name.Trim)
next
I also can get the canine groups written out on the second (so I know my model is working), however the URL.Action on that second "canines" page loses the "canines" in the URL so it renders as:
/animals/schnauzer
I've tried every way I can think of to write out the link including:
<a href="<%= url.action(canine.dog_name.Trim)) %>">
<a href="<%= Url.Content("~" & url.action(canine.dog_name.Trim)) %>">
and a few others that aren't worth showing. ;-D
... I'm guessing I'm missing something in a route path but what is it? It can't be that I have to know the path I'm at on every page in order to write out the URL - can it?
Thanks in advance for your help!
The route youve configured looks for three components, a controller, action and id.
In your case /animals/schnauzer, you're looking for more than a single route element as far as HTTP routing (and link generation), is concerned. It sees /animals/canines as two different values.
If you're looking to allow a route like http://yoursite/Family/Species, specify it as such:
routes.mapRoute("familyspecies", "animals/{family}/{species}", new { controller = "Home", action = "Animals", Species = UrlParameter.Optional});
and pass family and species in to your actions
public ActionResult Animals(string family, string species){...}
OR
Use wildcard routes to direct everything after the action to the id
routes.MapRoute("{controller}/{action}/{*id}");

Running ASP.NET MVC in a subdomain makes Html.ActionLink render broken links

I am running MVC in a subdomain
http://test.domain.com which points to the /Test directory on my webhost4life account.
Html.ActionLink("About", "About", "Home")
it renders a link to
http://test.domain.com/Test/Home/About -- which gives a 404
the link should be ..
http://test.domain.com/Home/About
is there a way to override ActionLink to omit the /Test on render?
Thank you
Experiment 1
I added a route to the table like this...
routes.MapRoute(
"Test", // Route name
"Test/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
and now action link renders links like this..
http://test.domain.com/Test/Test/Home/About/
when this is clicked it does not give a 404 but gives the Home controler About action.
Result
No more broken links but the site renders ugly urls.
For a site using lots of subdomains I use a nifty MVC extension from ITCloud called UrlRouteAttribute. It allows you to assign a route to every action as an attribute setting the path and name. I have extended this to allow fully qualified paths - so to include the domain/subdomain the controller should attach to. If this is something you'd be interested in I'll upload a copy somewhere.

Resources