繁体   English   中英

无法使新的控制器或视图在ASP.Net MVC中工作

[英]Can't get new controllers or views to work in ASP.Net MVC

标题基本上是怎么说的。 我创建了一个新的MVC应用程序。 我正在尝试向网站添加新页面,但是无论何时我都会遇到以下错误:

Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Products

这是我的控制器,称为ProductsController。

namespace MyAppMVC.Controllers
{
    public class ProductsController : Controller
    {
        public ActionResult Index()
        {
            return View("Index");
        }
    }
}

我也尝试过

            return View();

但这没有用。

我的视图称为Index.aspx,位于视图/产品文件夹中。

因此,一切对我来说都很好,我查看了NerdDinners教程,他们似乎也没有做任何与我不同的事情。 我已经看过了应用程序随附的家庭控制器,而我的控制器似乎完全一样。 我很确定我不必在路由中添加任何内容,但也许可以。

有任何想法吗? 这让我难过。

根据要求,这是我的global.asax.cs文件

public class MvcApplication : System.Web.HttpApplication
{
    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();

        RegisterRoutes(RouteTable.Routes);
    }
}

使用Phil Haack的路由测试器检查路由,并确保路由正确地路由到所需的控制器方法。

最后的问题是我创建了一个不正确的母版页,该母版页中包含导致问题的文件后面的代码。 我只是没有注意到MVC在“添加新”对话框中自己选择了“母版页”。 糟糕!

感谢大家的帮助。

可能是您正在使用IIS6而未启用通配符映射。 或者您可能还没有将默认文档设置为Default.aspx? 还是从应用程序根目录中删除了Default.aspx? :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM