简体   繁体   English

Razor 页面路由在 MVC 应用程序中不起作用

[英]Razor Page routing not working in MVC app

I have an MVC app to which I am trying to add some Razor pages.我有一个 MVC 应用程序,我正在尝试向其中添加一些 Razor 页面。 I have added the following page to the project root:我已将以下页面添加到项目根目录:

pages/account/index.cshtml

However I am unable to generate a URL to this page using the tag helpers like this:但是,我无法使用如下标签助手生成此页面的 URL:

<a  class="nav-link text-dark"  asp-page="/Account" title="Manage">Hello @User.Identity.Name!</a>

This results in this url: /Asset/List?page=%2FAccount%2FAccount这导致这个网址: /Asset/List?page=%2FAccount%2FAccount

Which is a url relative to the current page.这是相对于当前页面的 url。

However I am unable to browse to /account/index or /account in the browser, which tells me something is up with the routing.但是我无法在浏览器中浏览到/account/index/account ,这告诉我路由有问题。

My MVC controller routes are working fine, as are the default Razor pages created in the project template in the identity area, for example:我的 MVC 控制器路由工作正常,身份区域的项目模板中创建的默认 Razor 页面也是如此,例如:

identity/pages/account/manage/index

My configure method looks like this:我的配置方法如下所示:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
        });
    }

and ConfigureServices:和配置服务:

public void ConfigureServices(IServiceCollection services)
        {
            
            services.AddControllersWithViews().AddRazorRuntimeCompilation();
            services.AddRazorPages();
        }

I have an MVC app to which I am trying to add some Razor pages.我有一个 MVC 应用程序,我正在尝试向其中添加一些 Razor 页面。 I have added the following page to the project root:我已将以下页面添加到项目根目录:

pages/account/index.cshtml页面/帐户/index.cshtml

That will result in a route being generated of /account/ .这将导致生成/account/的路由。 The value passed to the asp-page attribute is the relative file path (rooted in the Pages folder) without the extension:传递给asp-page属性的值是不带扩展名的相对文件路径(以 Pages 文件夹为根):

<a asp-page="/account/index">Account Home </a>

More about routing in Razor Pages here: https://www.learnrazorpages.com/razor-pages/routing有关 Razor Pages 中路由的更多信息,请访问: https : //www.learnrazorpages.com/razor-pages/routing

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

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