如何在ASP.NET Core 2中设置默认主页? [英] How do I set a default home page in ASP.NET Core 2?

查看:175
本文介绍了如何在ASP.NET Core 2中设置默认主页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在配置ASP.NET MVC Core 2应用程序中的默认主页时遇到了一些困难.我似乎无法获得路由以映射到正确的首页,这是不使用任何参数时的默认设置,例如. https://localhost:44362

I am having some difficulty configuring a default home page in an ASP.NET MVC Core 2 application. I seem to be unable to get the routing to map to the correct home page a the default when no parameters are used, ex. https://localhost:44362

我真的不喜欢将Controllers放入Controllers文件夹,在Views中查看和在Models中查看模型的默认约定.我更喜欢按功能分组.

I don't really like the default convention of putting Controllers in a Controllers folder, view in Views, and View Models in Models. I prefer to group by feature.

所以我有一个这样的结构:

So I have a structure like this:

Features
   Home
      HomeController.cs
      HomeIndex.cshtml
      HomeViewModel.cs
   Other
      OtherController.cs
      OtherIndex.cshtml
      OtherViewModel.cs

一切正常,除了没有提供路径时,我似乎无法将默认页面设置为/Home/Index.当HomeController放在Controllers中时一切正常,而Views/Home中的Index则工作正常,但是一旦我将其移动,一切就坏了.

Everything works fine, except I can't seem to get the default page to be /Home/Index when no path is provided. Everything worked when HomeController was in Controllers (and Index in Views/Home), but as soon as I moved it, things broke.

我使用的是默认的Startup.cs文件,所以我有一个Configure方法,如下所示:

I'm using the default Startup.cs file, so I have a Configure method that looks like this:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseBrowserLink();
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseStaticFiles();

    app.UseAuthentication();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}

我做了一些搜索,它看起来像可以在ConfigureServices方法中使用:

I did some searching it and it looks like using this in the ConfigureServices method should work:

services.AddMvc().AddRazorPagesOptions(options =>
{
    options.Conventions.AddPageRoute("", "/Home/Index");
});

抛出一个错误:值不能为null或为空(pageName)异常.我也尝试使用"/","/Home/Index"作为参数.最后,出于很好的考虑,我还交换了上述参数,以防万一我误解了每个参数的用法.

That throws a: Value cannot be null or empty (pageName) exception. I also tried "/", "/Home/Index" as parameters. Finally, just for good measure, I also swapped the above parameters just in case I was misunderstanding usage of each parameter.

在每种情况下,都不会显示任何页面.如果我输入完整的URL/Home/Index,则可以.我还尝试将[Route("Home")]属性附加到HomeController类,并将[Route("Index"))]附加到操作方法.

In every case, no page is displayed. If I enter the full URL /Home/Index, it works. I also tried to attach a [Route("Home")] attribute to the HomeController class and a [Route("Index")] to the action method.

因此,我显然弄乱了路由,但是对我来说,如何解决这个问题一点儿也不清楚.我已经阅读并重新阅读了Razor Pages上的文档,但这似乎并不能消除我的理解.

So I'm obviously messed up the routing, but it's not at all clear to me how to fix this. I've read and re-read the docs on Razor Pages which doesn't seem to clear up my understanding.

推荐答案

@ paul-mrozowski,您在上述解决方案中几乎是正确的.对于基本页面,您必须使用以下方法来更改默认路由,前提是您的 Index 页面位于/Pages/Home/目录下:

@paul-mrozowski, you were almost correct in your solution above. For basic pages you'll have to use the following approach to change the default route, provided you Index page is under /Pages/Home/ directory:

services.AddMvc().AddRazorPagesOptions(options =>
{
    options.Conventions.AddPageRoute("/Home/Index", "");
});

只要有人使用Areas,只要您的 Index 页面位于/Areas/SomeArea/Pages/目录下,该方法就会类似:

Just in case someone is using Areas, an approach would be similar, provided your Index page is located under /Areas/SomeArea/Pages/ directory:

services.AddMvc().AddRazorPagesOptions(options =>
{
    options.Conventions.AddAreaPageRoute("SomeArea", "/Index", "");
});

这篇关于如何在ASP.NET Core 2中设置默认主页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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