ASP.Net MVC – Resource Cannot be found error

2019-01-10 18:38发布

I am completely new to ASP.Net MVC. I just created an MVC3 project in Visual Studio 2010. The view engine is razor. When I just ran the application it gave the proper result in the browser. The URL is http://localhost:4163/ . Then I applied “Set as Start Page” to Index.cshtml inside ~\Views\Home folder. Then when I ran the application the url became http://localhost:4148/Views/Home/Index.cshtml and it said the resource cannot be found. What do I do to correct it? Where is the url mapping done?

Global.asax file:

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

21条回答
看我几分像从前
2楼-- · 2019-01-10 19:10

For me the problem was caused by a namespace issue.

I had updated my project namespace in my web project classes from MyProject to MyProject.Web, but I had forgotten to update the Project Properties >> Default Namespace. Therefore when I added a new controller it was using the old namespace. All I had to do was correct the namespace in the new controller and it worked.

查看更多
贼婆χ
3楼-- · 2019-01-10 19:10

I had a controller named usersController , so I tried to create it the start page by changing default controller from Home to usersController like given below.

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

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "usersController", action = "Index", id = UrlParameter.Optional }
        );
    }

The solution was to change controller default value to users rather than usersController

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

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "users", action = "Index", id = UrlParameter.Optional }
        );
    }
查看更多
不美不萌又怎样
4楼-- · 2019-01-10 19:11

In a similar issue I faced, my Action had an HTTP "POST" attribute, but I was trying to open the page (by default it's "GET").

So I had to create an HTTP "GET" version.

查看更多
登录 后发表回答