Asp.net mvc 如何在Blazor WebAssembly托管解决方案上配置MVC

Asp.net mvc 如何在Blazor WebAssembly托管解决方案上配置MVC,asp.net-mvc,asp.net-core,blazor,Asp.net Mvc,Asp.net Core,Blazor,我有一个Blazor WebAssembly托管解决方案(客户端和服务器)设置,使用IdentityServer进行身份验证。我想做两件事 我想在服务器上设置MVC,因为我更喜欢MVC。服务器端页面的想法是用于配置文件管理和访问我不想在客户端上访问的内容 服务器Startup.cs当前具有 public void ConfigureServices(IServiceCollection services) { ....Condensed

我有一个Blazor WebAssembly托管解决方案(客户端和服务器)设置,使用IdentityServer进行身份验证。我想做两件事

  • 我想在服务器上设置MVC,因为我更喜欢MVC。服务器端页面的想法是用于配置文件管理和访问我不想在客户端上访问的内容
  • 服务器Startup.cs当前具有

    public void ConfigureServices(IServiceCollection services)
            {
                ....Condensed
    
                services.AddControllersWithViews();
                services.AddRazorPages();
            }
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, DataContext dataContext)
            {
               .....Condensed
    
               app.UseRouting();
    
                app.UseIdentityServer();
                app.UseAuthentication();
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapRazorPages();
                    endpoints.MapControllers();
                    endpoints.MapFallbackToFile("index.html");
                });
            }
    
    
  • 在后端安装MVC后,如何从客户端导航到这些页面

  • 创建WebAssembly解决方案时,请确保选中“ASP.Net Core Hosted”框

    这将创建三个项目:客户端、共享和服务器

    在服务器项目中,您将找到控制器文件夹。继续添加一个控制器,例如DummyController.cs

    namespace BlazorWASM4.Server.Controllers
    {
        [ApiController]
        [Route("[controller]")]
        public class DummyController : Controller
        {
            public IActionResult Index()
            {
                return View();
            }
        }
    }
    
    然后右键单击控制器方法索引并单击“添加视图”。然后实现如下视图(Index.cshtml),例如:

    <h1>Dummy Page</h1>
    
    虚拟页面
    
    运行项目并导航到localhost:port/Dummy


    您应该会看到显示的新页面。

    通常,当我想向应用程序添加框架时,我会使用VS或命令行创建我想要的类型的新空应用程序。然后我检查它自动生成的配置,并将相关配置逐件复制到我现有的应用程序中。我这样做了,它进入Blazor路由,并说找不到该页面。这就是我在这里问这个问题的最终原因,因为我认为它应该像你发布的那样工作。我想知道我是否需要在app.useEndpoints中更改任何内容。请注意,您确实将其标记为ApicController。我没有将它标记为APIController。我会试试看see@JoeyD,我的app.UseEndPoints代码与您在问题中发布的代码相同