ASP.NET MVC Remove query string in action method

别说谁变了你拦得住时间么 提交于 2019-11-29 12:17:01

问题


I have an action method that looks like this:

public ActionResult Index(string message)
{
  if (message != null)
  {
    ViewBag.Message = message;
  }
  return View();
}

What happens is that the url of a request to this will look like:

www.mysite.com/controller/?message=Hello%20world

But I want it to look just

www.mysite.com/controller/

Is there a way to remove the query string inside the actionmethod?


回答1:


No, unless you use a POST method, the information has to get passed somehow. An alternative may be to use an in-between class.

// this would work if you went to controller/SetMessage?message=hello%20world

public ActionResult SetMessage(string message)
{
  ViewBag.Message = message ?? "";
  return RedirectToAction("Index");
}

public ActionResult Index()
{
  ViewBag.Message = TempData["message"] != null ? TempData["message"] : "";
  return View();
}

Or. if you simply used a POST

//your view:
@using(Html.BeginForm())
{
    @Html.TextBox("message")
    <input type="submit" value="submit" />
}


[HttpGet]
public ActionResult Index()
{ return View(); }

[HttpPost]
public ActionResult Index(FormCollection form)
{
  ViewBag.Message = form["message"];
  return View();
}



回答2:


I'm not sure you're really thinking that through. If you remove the query string... then you remove the query string.. ie, your page won't have the query string value to do whatever it needs to do.

There's a bunch of different hacks you could do.. all of them are not ideal. You could use javascript to strip out the query string. You could redirect to a querystring-less page after setting a session variable.. it's all pretty ugly.

Remember that what the user sees in the address bar is on the client. The client controls that. You can fiddle with it via javascript, but doing so is generally a bad idea. Since hiding things from the user can be considered malware-like behavior.




回答3:


Look into routes. They define how a url with parameters will be written.

If you create a new MVC application, and look at the Global.asax.cs file under `RegisterRoutes(). you should see one entry.

routes.MapRoute(
   "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "home", action = "index", id = UrlParameter.Optional } // Parameter defaults
        );

Look at each part:

  • "Default" is the name. This just has to be unique for each route you create.
  • "{controller}/{action}/{id}" is the pattern you want to use. example.org/home/index?id=2 will be written example.org/home/index/2 instead
  • new { controller = "home", action = "index", id = UrlParameter.Optional } is defining the defaults if nothing is specified.

So, that route make it so if you go to example.org it will assume you mean example.org/home/index{id is optional}.

Working from that, you can start to see how to create your own routes.

Now, addressing your question the short answer is yes you could make the URL look like that, but not really. You would have to define a route with a default message, and it would only look like that if someone didn't specify a message. You have to tell the controller what the message is. I'm sorry, but the best you can do is define a route that gives you

/message/Hello%20World and using string.replace make that look even nicer `'/message/hello_world'




回答4:


I recommend using a slug. Check out this post: SOF Slug post In previous applications, I took this approach to remove querystrings from the URL.




回答5:


You can remove the query string by adding some JavaScript in the razor view.

@section scripts{
    <script>
        if (location.href.includes('?')) { 
            history.pushState({}, null, location.href.split('?')[0]); 
        }
    </script>    
}

If you navigate to page

www.mysite.com/controller/?message=Hello%20world

Then it'll show

www.mysite.com/controller/

in the browser.

Most modern browsers support this (browser support).



来源:https://stackoverflow.com/questions/9672843/asp-net-mvc-remove-query-string-in-action-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!