不能产生与在MVC @ Url.Action绝对URL [英] Not able to produce Absolute url with @Url.Action in MVC
问题描述
我MVC网站,我想使绝对URL与url.action但没有成功呢。
I have MVC website where i am trying to make absolute URL with url.action but not succeed yet.
下面是code,我使用:
Here is code i am using:
<li><a href="@Url.Action("Index", "Home")">Home</a></li>
<li><a href="@Url.Action("Contact","Home")">Contact</a></li>
主页和联系两者都是同样的观点。
Both Home and Contact are in same View.
因此,这里是错误的描述:当我运行的网站就说明这个网址和开放的网站非常完美。现在,如果我点击联系我们的链接它显示正确的网址:
So here is the error description: When I run the website it shows this url and open website perfectly. Now if I click on "Contact" link it shows correct URL:
http://localhost:5423/Home/Contact
之后,我点击首页链接它给了我错了网址:
After that I am clicking on "Home" Link it gives me wrong URL:
http://localhost:5423/Home/Home/Index
正确的URL应该是这样的:
Correct Url should be this:
http://localhost:5423/Home/Index
我不知道为什么它的固定家的网址。
I don't know why its retaining "Home" in URL.
这是我在写Global.asax.vb
This is what I wrote in Global.asax.vb
Public Class MvcApplication
Inherits System.Web.HttpApplication
Sub Application_Start()
AreaRegistration.RegisterAllAreas()
WebApiConfig.Register(GlobalConfiguration.Configuration)
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters)
RouteConfig.RegisterRoutes(RouteTable.Routes)
BundleConfig.RegisterBundles(BundleTable.Bundles)
AuthConfig.RegisterAuth()
End Sub
End Class
下面是code为RouteConfig:
Here is the code for RouteConfig:
Public Class RouteConfig
Public Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
routes.MapRoute( _
name:="Default", _
url:="{controller}/{action}/{id}", _
defaults:=New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional} _
)
End Sub
End Class
这是唯一我所面临的问题。
This is only the problem I am facing.
请给你的意见和建议。
推荐答案
右键,我想我是针对错误的事情。在你的 RouteConfig.cs
文件,广告新航线和
(道歉为C#,我不使用VB.NET了):
Right, I think I was targeting the wrong thing. In your RouteConfig.cs
file, ad new routes for Home/Index
and Home/Contact
(apologies for the C#, I don't use VB.NET anymore):
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "HomeLink",
url: "Home/Index",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "ContactLink",
url: "Home/Contact",
defaults: new { controller = "Home", action = "Contact" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
在你看来,创建这样的链接:
In your view, create the links like this:
<li><a href="@Url.RouteUrl("HomeLink")">Home</a></li>
<li><a href="@Url.RouteUrl("ContactLink")">Contact</a></li>
这样做应该可以解决您的网址的问题。为什么首页
正在出现两次,我不清楚,但创建自己的路线应该有所帮助。
Doing this should fix your url issue. Why Home
is appearing twice is unclear to me, but creating your own routes should help.
这篇关于不能产生与在MVC @ Url.Action绝对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!