不能产生与在MVC @ Url.Action绝对URL

编程入门 行业动态 更新时间:2024-10-25 14:35:55
本文介绍了不能产生与在MVC @ Url.Action绝对URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我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:

localhost:5423/Home/Contact

之后,我点击首页链接它给了我错了网址:

After that I am clicking on "Home" Link it gives me wrong URL:

localhost:5423/Home/Home/Index

正确的URL应该是这样的:

Correct Url should be this:

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 文件,

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

本文发布于:2023-11-03 20:39:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556037.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:生与   MVC   Url   Action   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!