ASP.NET MVC中的操作方法取出查询字符串 [英] ASP.NET MVC Remove query string in action method

查看:158
本文介绍了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

但我希望它只是看看

But I want it to look just

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天全站免登陆