0

We have an ASP.NET Core 2 MVC application and an action method Search which normally returns an IActionResult in the following form:

return View("SearchResult", Model);

How can we manipulate the return url?

What we would like to do is to take the QueryString and Add/Remove various keys using the QueryHelpers and other built-in functionalities and then change the return Url to that with the new QueryString.

If we just leave return View("SearchResult", Model); the original Url is used and not the changed one.

Any assistance would be greatly appreciated.

4
  • 1
    I think you may mean to return RedirectToAction("yourAnotherActionName","yourAnotherControllerName");? Commented Sep 2, 2019 at 22:44
  • Please do not supply answers that are completely useless! Commented Sep 3, 2019 at 0:51
  • 1
    What do you expect to get? Please share some code example or screenshot that can make us to better understand what you mean. Commented Sep 3, 2019 at 9:08
  • Lets say your request url is: /controller/action?type=10&size=20&color=blue and you want to send back client to controller/action?type=10&size=20 (removing 1 query paramter) but with a model as stated above. Commented Sep 3, 2019 at 11:25

1 Answer 1

1

You could not control the web browser url directly from server side.

For a workaround, you could try to URL Rewriting Middleware in ASP.NET Core like

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.AddSession();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {

        app.UseSession();
        var options = new RewriteOptions()
       .Add(context => {
           var request = context.HttpContext.Request;
           var query = request.Query;
           StringValues color = "";
           if (request.Path.StartsWithSegments("/Home/Test", StringComparison.OrdinalIgnoreCase)
           && query.TryGetValue("color", out color))
           {
               var response = context.HttpContext.Response;
               response.StatusCode = StatusCodes.Status301MovedPermanently;
               context.Result = RuleResult.EndResponse;
               context.HttpContext.Session.SetString("query", request.QueryString.Value);

               var items = query.SelectMany(x => x.Value, (col, value) => new KeyValuePair<string, string>(col.Key, value)).ToList();
               // At this point you can remove items if you want
               items.RemoveAll(x => x.Key == "color"); // Remove all values for key
               var qb = new QueryBuilder(items);
               response.Headers[HeaderNames.Location] =
                   request.Path + qb;
           } 
           else if (request.Path.StartsWithSegments("/Home/Test", StringComparison.OrdinalIgnoreCase)
           && !query.TryGetValue("color", out color))
           {
               context.Result = RuleResult.SkipRemainingRules;
               request.QueryString = new QueryString(context.HttpContext.Session.GetString("query"));
           }
       });

        app.UseRewriter(options);            

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.