ASP.Net MVC – 无法找到资源错误 [英] ASP.Net MVC – Resource Cannot be found error

查看:42
本文介绍了ASP.Net MVC – 无法找到资源错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 ASP.Net MVC 完全陌生.我刚刚在 Visual Studio 2010 中创建了一个 MVC3 项目.视图引擎是 razor.当我刚刚运行应用程序时,它在浏览器中给出了正确的结果.URL 是 http://localhost:4163/ .然后我将设置为起始页"应用于 ~ViewsHome 文件夹中的 Index.cshtml.然后当我运行应用程序时,url 变成了 http://localhost:4148/Views/Home/Index.cshtml 并说找不到资源.我该怎么做才能纠正它?url映射在哪里做的?

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 ~ViewsHome 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 文件:

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

推荐答案

URL 映射或路由"由 ASP.NET MVC 站点根目录中的 Global.asax 处理.

URL mapping or "routing" is handled by Global.asax in the root of your ASP.NET MVC site.

当您单击设置为起始页"时,它会更改项目设置以查找相对于应用程序根目录的文件.但是在 MVC 中,默认 到您的索引页面的路由实际上是 http://localhost:4163/Home/Index - 阅读类似 这个 了解路由的工作原理.

When you click "Set as Start Page" it changes the project settings to look for that file relative to the application root. But in MVC the default route to your index page is actually http://localhost:4163/Home/Index - read something like this to get an idea of how routing works.

要修复"您的项目,因为它正在尝试(但失败)直接导航到视图,请右键单击该项目并选择Properties",单击Web代码>"选项卡并选择<代码>特定页面",将文本框留空.现在,当您开始调试时,它应该再次转到主页 - 查看默认路由参数以了解 Global.asax 中的 RegisterRoutes 方法中的原因

To "fix" your project now that it's trying (and failing) to navigate to the view directly, right click the project and choose "Properties", click the "Web" tab and choose "Specific Page", leaving the text box blank. Now when you start to debug it should go to the home page again - look at the default route parameters to see why in the RegisterRoutes method in Global.asax

这篇关于ASP.Net MVC – 无法找到资源错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆