Outgoing Routing in asp.net mvc - 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

Related

Get url for custom route in 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" })

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!

Why doesn't Default route work using Html.ActionLink in this case?

I have a rather peculiar issue with routing.
Coming back to routing after not having to worry about configuration for it for a year, I am using the default route and ignore route for resources:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
// Route name
"{controller}/{action}/{id}",
// URL with parameters
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
});
I have a RulesController with an action for Index and Lorem and a Index.aspx, Lorem.aspx in Views > Rules directory.
I have an ActionLink aimed at Rules/Index on the maseter page:
<li><div><%: Html.ActionLink("linkText", "Index", "Rules")%></div></li>
The link is being rendered as http://localhost:12345/Rules/ and am getting a 404.
When I type Index into the URL the application routes it to the action.
When I change the default route action from "Index" to "Lorem", the action link is being rendered as http://localhost:12345/Rules/Index adding the Index as it's no longer on the default route and the application routes to the Index action correctly.
I have used Phil Haack's Routing Debugger, but entering the url http://localhost:12345/Rules/ is causing a 404 using that too.
I think I've covered all of the rookie mistakes, relevant SO questions and basic RTFMs.
I'm assuming that "Rules" isn't any sort of reserved word in routing.
Other than updating the Routes and debuugging them, what can I look at?
Make sure there is not a folder called 'Rules' in the same directory as your website. In its default configuration, ASP.NET MVC routes will respect physical paths before route definitions. If there is a route defined which matches the path to a physical folder in the website, the routing engine will be bypassed completely.
You can disable routing to physical paths by changing the RouteTable.Routes.RouteExistingFiles property to false, but if you do this and your application has paths to physical resources (such as images, scripts, stylesheets, etc) you will need to accommodate for those paths with matching IgnoreRoute() definitions.
For example: RouteTable.Routes.IgnoreRoute("content/{*pathInfo}");.

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.

ASP.NET MVC - Is it possible to generate cross-application action links?

is it possible to generate cross-application action links using the HTML helper class?
I have the following (both are separate VS2008 projects):
http://mainwebsite/
http://mainwebsite/application
I would like to generate a link IN the /mainwebsite/application/ project TO /mainwebsite/. Is this possible? Or should I just hardcode a hyperlink?
addition:
My question also applies vice-versa. So generating a link IN /mainwebsite/ TO /mainwebsite/application/. I already managed to do like so, by simulating the application name as controller name:
<% =Html.ActionLink("ApplicationName","",new With {.Controller = "application" }) %>
Yes, you can do this. The HTML helper generates URLs using the routing system, which is completely ignorant of ASP.NET MVC and your application. If you have a route in your route table for the other application, then you can generate a URL for it using the HTML helper. There is no rule which states that your route table can only contain routes for the current application, although you obviously need to be careful about how you order the routes.

Resources