在visualstudio上运行一个网站项目失败,在本地被http 404找不到页面错误



我一直在做一个项目,我离开它大约一周了,当我回到它并尝试用Ctr+F5运行该项目时,它重定向到了一个未在路由配置中定义的URL。像这样的http://localhost:53771/Views/Home/Index.cshtml,并面临HTTP 404:未找到错误。虽然我的路由配置是这样的,但如果我自己在r+F5前面键入CORRET URL,它会重定向到路由配置中没有定义的URL。像这个http://localhost:53771

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

我开始调试这个项目,似乎这个项目从来没有出现在我的HomeController中。我只有一个名为Home的控制器,当我删除Home控制器的所有代码时,它的行为仍然相同。另一件似乎是错误的事情是,它甚至在到达RegisterRoutes函数之前就创建了URLhttp://localhost:53771/Views/Home/Index.cshtml。这个问题都是本地的,如果需要的话,这里是我的global.asax。我感谢的任何帮助

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace ML_Projectname
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
MvcHandler.DisableMvcResponseHeader = true;
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
MvcHandler.DisableMvcResponseHeader = true;

}

protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
HttpContext.Current.Response.Headers.Remove("X-Powered-By");
HttpContext.Current.Response.Headers.Remove("X-AspNet-Version");
HttpContext.Current.Response.Headers.Remove("X-AspNetMvc-Version");
HttpContext.Current.Response.Headers.Remove("Server");
}
}
}

我认为您的路由或应用程序没有任何问题。Visual Studio的默认启动操作(通过右键单击项目->选择属性->选择Web来查看(是"当前页面"。如果您在visualstudio中运行项目时正在查看剃刀视图(例如/Views/Home/Index.cshtml(,visualstudio将尝试直接导航到此资源。在MVC应用程序的上下文中,直接请求剃刀视图是无效的,您最终会看到404错误。

为了避免这种情况,您可以将启动url设置为特定的url(例如:http://localhost:53771(,或者更简单地,在通过Visual Studio运行项目之前,打开并查看其中一个服务器端文件(例如:HomeController.cs(。

tldr版本:这是visualstudio中设置的问题,而不是应用程序代码的问题

最新更新