gpt4 book ai didi

ASP.Net MVC – 找不到资源错误

转载 作者:行者123 更新时间:2023-12-03 00:06:58 24 4
gpt4 key购买 nike

我对 ASP.Net MVC 完全陌生。我刚刚在 Visual Studio 2010 中创建了一个 MVC3 项目。 View 引擎是 razor。当我刚刚运行该应用程序时,它在浏览器中给出了正确的结果。网址是http://localhost:4163/ 。然后我将“设置为起始页”应用于 ~\Views\Home 文件夹中的 Index.cshtml。然后,当我运行该应用程序时,网址变为 http://localhost:4148/Views/Home/Index.cshtml并且说找不到资源。我该怎么做才能纠正它? url映射是在哪里完成的?

Global.asax 文件:

using System.Web.Mvc;
using System.Web.Routing;

namespace TEST
{

public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

}

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
}

最佳答案

URL 映射或“路由”由 ASP.NET MVC 站点根目录中的 Global.asax 处理。

当您单击“设置为起始页”时,它会更改项目设置以查找相对于应用程序根目录的文件。但在 MVC 中,到索引页的默认路由实际上是http://localhost:4163/Home/Index - 阅读类似 this 的内容。了解路由的工作原理。

要“修复”您的项目,因为它正在尝试(但失败)直接导航到 View ,请右键单击该项目并选择“Properties”,然后单击“Web code>”选项卡并选择“特定页面”,将文本框留空。现在,当您开始调试时,它应该再次转到主页 - 查看默认路由参数以了解 Global.asax 中 RegisterRoutes 方法中的原因

关于ASP.Net MVC – 找不到资源错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9293778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com