ASP.NET Core 奇怪的 URL 解析单个查询字符串参数

问题描述

我想将字符串参数传递给操作。使用以下签名在 HomeController 中创建了一个方法:

[HttpGet]
public IActionResult TestView([FromQuery] string test)
{
    return View(test);
}

这是我的配置类:

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            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.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }

当我去 https://localhost:5001/Home/TestView 时它工作正常 当我添加查询字符串 ?test=myvalue 时,它​​无法找到视图。它尝试使用奇怪的路径定位视图。

    InvalidOperationException: The view 'myvalue' was not found. The following locations were searched:
    /Views/Home/myvalue.cshtml
    /Views/Shared/myvalue.cshtml

这是一个错误吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)