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?}");
});
}
}
return RedirectToAction("yourAnotherActionName","yourAnotherControllerName");?