0

I want to pass a string parameter to an action. Acreated a method in the HomeController with the following signature:

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

This is my configuration class:

    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?}");
            });
        }
    }

When I go to https://localhost:5001/Home/TestView it works ok When I add a query string ?test=myvalue it fails to find the view. It tries to locate the view using weird paths.

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

Is that a bug?

2
  • what's your middlewares in Startup.cs? Commented Jan 25, 2021 at 12:30
  • @Jacek I posted the whole Startup.cs. Though, I haven't touched it. It's from default MVC templete Commented Jan 25, 2021 at 12:35

1 Answer 1

2

The behavior is occurring because you passed the value "myvalue" as the first parameter of your returned ViewResult, which is the viewname parameter:

https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.viewresult.viewname?view=aspnetcore-5.0

If you change your return statement to:

return View();

Then you won't be passing a view name parameter and it will then search for a view named TestView.

Sign up to request clarification or add additional context in comments.

2 Comments

No worries bro. If you were expecting to see "myvalue" in the browser, change your return type to string and then can return test, bypassing views altogether. Or you could instead bind the value to the view. Just depends what you want to do.
@SergеуIsupov the first argument is the view name, if you want to pass something to the view, pass it to the second argument. So in your case, you can write View("TestView", test) - the view model then is of type string which should be aware of by the view to use it correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.