2

我在我的网站中添加了一个名为博客的区域我创建了所有内容,我只是尝试通过手动输入 URL 在浏览器中访问它,但我收到了类似“'/' 应用程序中的服务器错误。”的错误。我附上了我的项目的代码和快照。任何帮助,将不胜感激。

全球阿萨克斯

public static void MyCustomRouting(RouteCollection coll)
{
    coll.IgnoreRoute("{resource}.axd/{*pathInfo}");
    coll.MapRoute("Default", "{controller}/{action}", new { controller = "Home", action = "Index" }, new[] { "Areas.Controllers" });
}

protected void Application_Start()
{
    //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
    AreaRegistration.RegisterAllAreas();
    RegisterGlobalFilters(GlobalFilters.Filters);
    MyCustomRouting(RouteTable.Routes);
}

BloggingAreaRegistration.cs

using System.Web.Mvc;

namespace MVC_PageRouting.Areas.Blogging
{
    public class BloggingAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get { return "Blogging"; }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute("Blogging_default", "Blogging/{controller}/{action}/{id}", new {action="Index",UrlParameter.Optional });
        }
    }
}

文件夹结构:

建筑学

错误:

浏览器错误

4

3 回答 3

2

通过在 UrlParameter.Optional 前面添加“id =”来指定您的 id 参数是可选的

new {action="Index", id = UrlParameter.Optional }
于 2013-09-04T06:46:24.800 回答
1

我敢打赌,如果转到此网址,它会起作用:

http://localhost:51803/Blogging/BloggingHome/Index/0

原因是您错误地指定了 MapRoute 参数。你指定了这个:

new {action="Index", UrlParameter.Optional });

您忘记包含 id 名称,因此 MVC 不知道您的意思。你要这个:

new {action="Index", id=UrlParameter.Optional });

注意“id=”,它告诉 MVC id 是可选的。没有它,MVC 不知道 UrlParameter.Optional 是什么意思,因此它需要 id。由于您没有在 URL 中包含 id,因此没有选择任何路由,因此发出了 404。

于 2013-09-04T06:51:25.910 回答
0

您可以将您的区域注册更新为此并尝试吗?

context.MapRoute("Blogging_default", "Blogging/{controller}/{action}/{id}", new {controller = "BloggingHome",action="Index",UrlParameter.Optional });
于 2013-09-04T04:35:52.450 回答