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 18:45

In asp.net mvc you can't use option 'set as start page', because mvc views is not independent, like web forms pages. It is only template files for displaying your model. They have no processing http module. All web requests should pass through controller actions, you can't request views directly.

查看更多
看我几分像从前
3楼-- · 2019-01-10 18:48

Well you cannot set the default page in asp.net mvc.
Go to global.asax.cs and see the definition of routing. The default route points to Index method HomeController.
You'll better watch some short movies about asp.net mvc or try to find nerd dinner tutorial which will make you familiar with the framework pretty quickly.

I think the best answers about tutorials were already provided as answers to this question:

ASP.NET MVC Quick Tutorials

查看更多
爷、活的狠高调
4楼-- · 2019-01-10 18:48

You can change the start page inside of your project's properties (project -> project properties). Under the web tab you can select specify page and change your start page to home/ or home/index. This will direct you to the proper page according to your routes. The address of http://localhost:4148/Views/Home/Index.cshtml doesn't work specifically because it is being handled according to your routes which state that the url needs to be http://localhost:4148/{controller}/{action} and an optional /{id}.

If you want to learn more about routing take a look here.

查看更多
Emotional °昔
5楼-- · 2019-01-10 18:48

I had the same sort of issue, except I was not using the name Index.cshtml for my View, I made it for example LandingPage.cshtml, my problem was that the ActionResult in my Controller was not the same name as my View Name(In this case my ActionResult was called Index()) - This is actually obvious and a silly mistake from my part, but im new to MVC, so I will fail with the basics here and there.

So in my case I needed to change my ActionResult to be the same name as my View : LandingPage.cshtml

    public ActionResult LandingPage()
    {
        ProjectDetailsViewModels PD = new ProjectDetailsViewModels();
        List<ProjectDetail> PDList = new List<ProjectDetail>();

        PDList = GetProductList();
        PD.Projectmodel = PDList;


        return View(PD);
    }

I am new to MVC, so perhaps this will help someone else who is new and struggling with the same thing I did.

查看更多
乱世女痞
6楼-- · 2019-01-10 18:53

asp.net mvc project's start page is "Current Page" defaulty. Just open an server-side file and start project. in that way mvc open the default controller which is homecontroller for your project

查看更多
Emotional °昔
7楼-- · 2019-01-10 18:55

Make sure you created a HomeController.cs class in your controller folder.

查看更多
登录 后发表回答