为什么 IIS 中的 href URL 显示"HTTP Error 404.0 - Not Found"错误?(ASP.NET MVC C#)



我向IIS发布了我的ASP.NET MVC Web应用程序,它确实打开了我的前两个页面(登录和主页(,然而,在我的导航栏中,我有多个带有href的菜单(链接<a>(。(例如href="/Users/Index"(

当我在浏览器中打开我的应用程序时,它显示:http://localhost/AppName/Home

当我点击导航栏上的菜单项时,它不会添加appname,也不会呈现我的视图:http://localhost/User/Index

并发送HTTP错误404.0-未找到

显然,如果我手动添加AppName,它确实会打开视图。

这在VSIDE中不会发生,但当我将其发布到IIS时,路由似乎不起作用。

有什么想法吗??

RouteConfig.cs

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
}

导航栏上的链接

<a href="/User/Index" class="dropdown-toggle"> Users</a>

我的web.config确实有:

<system.webServer>
<modules runAllManagedModulesForAllRequests="true"></modules>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>

更新!

根据VeNoMiS的建议,我在所有导航栏菜单项中插入了Url.Action,将我从数据库中获得的Url拆分为每个参数(视图、控制器、区域(。

IIS中的所有路由都运行良好。

<a href="@Url.Action(Url.Split('/')[2], Url.Split('/')[1], new { Area = Url.Split('/')[3] })"><span class="@Icon" title="@Icon"></span> @Name</a>

干杯

相关内容

最新更新