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

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

问题描述

我是完全新的ASP.Net MVC。我刚刚创建Visual Studio 2010中的MVC3项目视图引擎是剃须刀。当我刚刚运行的应用程序它给了在浏览器中正确的结果。网址是的http://本地主机:4163 / 。然后,我申请设置为起始页,以Index.cshtml里面〜\\查看\\ Home文件夹。然后,当我跑的URL成为应用程序的http://localhost:4148/Views/Home/Index.cshtml它说,资源不能被发现。我该怎么做来纠正呢?哪里URL映射呢?

Global.asax文件:

 使用System.Web.Mvc;
使用System.Web.Routing;命名空间TEST
{公共类MvcApplication:System.Web.HttpApplication
{
    公共静态无效RegisterGlobalFilters(GlobalFilterCollection过滤器)
    {
        filters.Add(新HandleErrorAttribute());
    }    公共静态无效的RegisterRoutes(RouteCollection路线)
    {
        routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);        routes.MapRoute(
            默认,//路线名称
            {控制器} / {行动} / {ID},// URL带参数
            新{控制器=家,行动=索引,ID = UrlParameter.Optional} //参数默认
        );    }    保护无效的Application_Start()
    {
        AreaRegistration.RegisterAllAreas();        RegisterGlobalFilters(GlobalFilters.Filters);
        的RegisterRoutes(RouteTable.Routes);
    }
    }
 }


解决方案

URL映射或路由是在Global.asax中的ASP.NET MVC站点的根处理。

当您单击设置为起始页它改变了项目设置,寻找相对于应用程序根目录文件。但在MVC你的索引页中的默认的航线实际上是的http://本地主机:4163 /首页/指数 - 读取类似<一个href=\"http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs\">this获得如何路由工作的想法。

要现在修复项目,它的尝试(和失败)直接导航到该视图中,右键单击该项目,然后选择属性,单击 网​​站选项卡,然后选择具体页,使文本框为空。现在,当你开始调试它应该再去主页 - 看一下默认路由参数,看看为什么在Global.asax中的RegisterRoutes方法

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

解决方案

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

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.

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天全站免登陆