ASP.NET MVC 在操作方法中删除查询字符串 [英] ASP.NET MVC Remove query string in action method

查看:21
本文介绍了ASP.NET MVC 在操作方法中删除查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的操作方法:

I have an action method that looks like this:

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

所发生的情况是,对此的请求的 url 将如下所示:

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

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

但我希望它看起来只是

www.mysite.com/controller/

有没有办法删除 actionmethod 中的查询字符串?

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

推荐答案

不,除非您使用 POST 方法,否则信息必须以某种方式传递.另一种方法是使用中间类.

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

或者.如果您只是使用 POST

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

这篇关于ASP.NET MVC 在操作方法中删除查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆