簡體   English   中英

在Visual Studio上運行網站項目在本地失敗,無法通過http 404找到頁面錯誤

[英]run a website project on visual studio failed in local by http 404 not found page error

我一直在從事一個項目,當我回到該項目並嘗試使用Ctr + F5運行該項目時,我將其保留了大約一周的時間,該項目將重定向到未在route config中定義的URL。 像這樣的http://localhost:53771/Views/Home/Index.cshtml並遇到HTTP 404:找不到錯誤。 雖然我的路由配置像這樣,但如果我在r + F5前面輸入CORRET URL MYSELF,它就可以正常工作,但它會重定向到路由配置中未定義的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函數之前,它都會創建URL http://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進行查看)是“當前頁面”。 如果您在Visual Studio中運行項目時正在查看剃刀視圖(例如/Views/Home/Index.cshtml Studio將嘗試直接導航至該資源。 直接請求剃刀視圖在MVC應用程序的上下文中無效,最終您會看到404錯誤。

為了避免這種情況,您可以將起始URL設置為特定的URL(例如: http://localhost:53771 ),或更簡單的方法是,在通過運行項目之前,打開並查看服務器端文件之一(例如: HomeController.cs )。視覺工作室。

tldr版本:這是Visual Studio中設置的問題,而不是應用程序代碼的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM