neda Derakhshesh
neda Derakhshesh

Reputation: 1163

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

I have been working on a project and I left it for about one week,when I came back to it and I tried to run the project with Ctr+F5 , it redirect to a URL which is not defined in route config. like this http://localhost:53771/Views/Home/Index.cshtml and confronted with HTTP 404: not found Error. while my route config is like this BUT IT WORKS FINE IF I TYPE THE CORRET URL MYSELF in front of the r+F5 , it redirect to a URL which is not defined in route config. like this 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 }
           );
    }

I started debugging the project and it seems the project never come to my HomeController . I just have one controller named Home, and when I removed all the codes of my Home controller it still act the same. and the other thing which it seems wrong, it creates the URL http://localhost:53771/Views/Home/Index.cshtml even before it comes to RegisterRoutes function. the problem is all local and here is my global.asax if needed. I appreciate any help

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");

        }
    }
}

Upvotes: 0

Views: 2584

Answers (1)

GregH
GregH

Reputation: 5457

I don't believe there is any issue with your routing or your application. Visual Studio's default start action (view by right clicking project -> selecting properties -> selecting Web) is "Current Page". If you're viewing a razor view (such as your /Views/Home/Index.cshtml) at the time you run the project in visual studio, visual studio will try to navigate to this resource directly. Directly requesting a razor view is not valid in the context of an MVC application and you end up seeing the 404 error.

To avoid this, you can set the start url to a specific URL (ex: http://localhost:53771) or even simpler, open and view one of the server side files (ex: HomeController.cs) before running your project via Visual Studio.

tldr version: this is an issue with a setting you have in visual studio, not an issue with your application code

Upvotes: 2

Related Questions