Url.Action()
uses the routing to generate the URL. so if you want to change it, you must change that. It currently says the default controller is Home
and default action is Index
. Change them to anything else and it should then give you a different URL.
For example your route configuration is probably something like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Change the defaults to anything else or remove them:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
Note that doing this means your pages will only be accessible by full controller/action
paths so you may want to create a landing page and make that the default.
If you absolutely need to know the full URL of an action then you can do it this way. first create an additional route and put it at the bottom of your route config. This will never get used by the system by default:
routes.MapRoute(
name: "AbsoluteRoute",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
Then in code you can call this (not sure it's available in Razor, but it should be easy to write a helper method):
var fullURL = UrlHelper.GenerateUrl("AbsoluteRoute", "Index", "Home", null, null, null, null, System.Web.Routing.RouteTable.Routes, Request.RequestContext, false);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…