ASP.NET MVC 5路由,在URL中隐藏数据 [英] ASP.NET MVC 5 routing, hide data in URL

查看:192
本文介绍了ASP.NET MVC 5路由,在URL中隐藏数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于具有 bool 变量的操作,我有两种功能。

I have two types of functionality for a action with a bool variable.

[HttpGet]
public ActionResult action(bool data = false)
{
   if(data == false)
   {
      return View("view1");
   }
   else
   {
      return View("view2");
   }
}

这是 [httpGet ] 方法。某些链接的数据布尔值为 true ,有些链接的 false

网址具有像 http:// localhost:58241 / action?data = False

这样的属性,我想隐藏?data = False ?data = True 并应具有与以前相同的所有功能。

It is a [httpGet] method. some link has data bool value as true and some has false.
The url has the attribute like http://localhost:58241/action?data=False
I want to hide the ?data=False or ?data=Truefrom URL and should possess all the same functionality like before.

我想要类似 http:// localhost:58241 / action

的URL。

推荐答案

路由与查询字符串参数完全没有关系。无论如何,您仍然需要将 data 参数传输到服务器,以便action方法接收它。可以通过3种方式进行此操作:

Routing has absolutely nothing at all to do with query string parameters. And in any case, you still need to transfer the data parameter to the server for the action method to receive it. There are 3 ways to do this:


  1. 使用HTTP GET(作为路由值或查询字符串值)在URL中传递
  2. 使用HTTP POST将其传递到表单正文中

  3. 将其传递到模型中(使用HTTP POST和模型绑定)

最简单的选择是#1,但是由于您提到不能通过URL传递数据,因此唯一的选择是使用HTTP发布。因此,此答案的其余部分使用#2。

The simplest option is #1, however since you mentioned this isn't acceptable to pass the data through the URL, your only choice is to use HTTP post. So, the rest of this answer uses #2.

首先,默认路由不会覆盖您选择的URL( / action ),因此您需要为此插入自定义路由。

First, the default route does not cover your choice of URL (/action), so you need to insert a custom route for this.

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        // Add route to handle /action
        routes.MapRoute(
            name: "Action",
            url: "action",
            defaults: new { controller = "Data", action = "Action" }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

接下来,您需要一个控制器来处理这两个GET

Next, you need a controller to handle both the GET and POST from the browser.

public class DataController : Controller
{
    public ActionResult Action()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Action(bool data = false)
    {
        if (data)
        {
            return View("view2");
        }

        return View("view1");
    }
}

数据在POST中发送回服务器,因此不需要在URL中传递它。

The data is sent back to the server in the POST, so it is not required to pass it in the URL.

最后,您有了视图(名为 Action.cshtml )是从 Action 操作方法返回的。它具有2个不同的表单标签,这些表单标签根据所单击的按钮为 data 提交不同的值。

Finally, you have the view (named Action.cshtml) that is returned from the Action action method. It has 2 different form tags that submit a different value for data depending on the button clicked.

@{
    ViewBag.Title = "Action";
}

<h2>Choose an Option</h2>

@using (Html.BeginForm("action", "Data")) {

    <input type="hidden" name="data" value="true" />
    <input type="submit" value="With Data" />
}

@using (Html.BeginForm("action", "Data")) {

    <input type="hidden" name="data" value="false" />
    <input type="submit" value="Without Data" />
}

请注意,如果您可以完全在JavaScript(AJAX POST)中执行此步骤请,这将使您能够使用超链接而不是按钮,或者可以使用CSS设置按钮的样式,使其看起来像是超链接。

Note that you could do this step entirely in JavaScript (AJAX POST) if you please, which would enable you to use a hyperlink instead of a button or you could just style the button using CSS to look like a hyperlink.

这篇关于ASP.NET MVC 5路由,在URL中隐藏数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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