ASP.NET MVC的QueryString中重写默认提供的值? [英] ASP.NET MVC QueryString defaults overriding supplied values?

查看:129
本文介绍了ASP.NET MVC的QueryString中重写默认提供的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ASP.NET MVC preVIEW 5(虽然这也试图与测试版),它出现在路线查询字符串默认覆盖传递的查询字符串的值。一个摄制是编写一个这样的控制器:

 公共类的TestController:控制器
{
    公众的ActionResult美孚(INT X)
    {
        Trace.WriteLine(X);
        Trace.WriteLine(this.HttpContext.Request.QueryString [X]);
        返回新EmptyResult();
    }
}

使用映射为路径如下:

  routes.MapRoute(
    测试,
    测试/富,
    新{控制器=测试,行动=富,X = 1});

和则与此相对的URI调用它:

  /测试/美孚X = 5

跟踪输出我看到的是:

  1

因此​​,换句话说,这是建立路由的默认值总是传递到方法,不论其是否在查询字符串实际上提供。请注意,如果对查询字符串的默认被除去,即,路由被如下映射

  routes.MapRoute(
    测试,
    测试/富,
    新{控制器=测试,行动=富});

然后,控制器的行为与预期值传递的参数值,给跟踪输出:

  5

这在我看来就像一个错误,但我会觉得很奇怪,像这样的错误仍然可以在ASP.NET MVC框架的测试版,与默认查询字符串不完全深奥的或边缘 - 案例的功能,所以它几乎肯定我的错。任何想法我做错了吗?


解决方案

来看看ASP.NET MVC与查询字符串的最好方法是把它们作为该路由不知道值。当你发现,QueryString中是没有的RouteData的一部分,因此,你应该让你正在传递什么作为查询字符串从路由值是分开的。

要解决它们的一种方法是自己在行动创建默认值,如果从查询字符串传递的值都为null。

在你的榜样,路线知道X,所以您的网址确实应该是这样的:

  /测试/美孚或/测试/美孚/ 5

和路线应该是这样的:

  routes.MapRoute(测试,测试/美孚/ {X},新{控制器=测试,行动=富,X = 1});

要得到你要找的人的行为。

如果你想传递一个查​​询字符串值,说喜欢页码,那么你会做这样的:

  /测试/美孚/ 5?页= 1

和你的行动应该改变这样的:

 公众的ActionResult美孚(INT X,诠释?页)
{
    Trace.WriteLine(X);
    Trace.WriteLine(page.HasValue page.Value:1);
    返回新EmptyResult();
}

现在测试:


 网​​址:/测试/美孚
跟踪:
1
1网址:/测试/美孚/ 5
跟踪:

1网址:/测试/美孚/ 5页= 2
跟踪:

2网址:/测试/美孚页= 2
跟踪:
1
2


希望这有助于澄清一些事情。

Using ASP.NET MVC Preview 5 (though this has also been tried with the Beta), it appears that querystring defaults in a route override the value that is passed in on the query string. A repro is to write a controller like this:

public class TestController : Controller
{
    public ActionResult Foo(int x)
    {
        Trace.WriteLine(x);
        Trace.WriteLine(this.HttpContext.Request.QueryString["x"]);
        return new EmptyResult();
    }
}

With route mapped as follows:

routes.MapRoute(
    "test",
    "Test/Foo",
    new { controller = "Test", action = "Foo", x = 1 });

And then invoke it with this relative URI:

/Test/Foo?x=5

The trace output I see is:

1
5

So in other words the default value that was set up for the route is always passed into the method, irrespective of whether it was actually supplied on the querystring. Note that if the default for the querystring is removed, i.e. the route is mapped as follows:

routes.MapRoute(
    "test",
    "Test/Foo",
    new { controller = "Test", action = "Foo" });

Then the controller behaves as expected and the value is passed in as the parameter value, giving the trace output:

5
5

This looks to me like a bug, but I would find it very surprising that a bug like this could still be in the beta release of the ASP.NET MVC framework, as querystrings with defaults aren't exactly an esoteric or edge-case feature, so it's almost certainly my fault. Any ideas what I'm doing wrong?

解决方案

The best way to look at ASP.NET MVC with QueryStrings is to think of them as values that the route does not know about. As you found out, the QueryString is not part of the RouteData, therefore, you should keep what you are passing as a query string separate from the route values.

A way to work around them is to create default values yourself in the action if the values passed from the QueryString are null.

In your example, the route knows about x, therefore your url should really look like this:

/Test/Foo or /Test/Foo/5

and the route should look like this:

routes.MapRoute("test", "Test/Foo/{x}", new {controller = "Test", action = "Foo", x = 1});

To get the behavior you were looking for.

If you want to pass a QueryString value, say like a page number then you would do this:

/Test/Foo/5?page=1

And your action should change like this:

public ActionResult Foo(int x, int? page)
{
    Trace.WriteLine(x);
    Trace.WriteLine(page.HasValue ? page.Value : 1);
    return new EmptyResult();
}

Now the test:

Url:  /Test/Foo
Trace:
1
1

Url:  /Test/Foo/5
Trace:
5
1

Url:  /Test/Foo/5?page=2
Trace:
5
2

Url:  /Test/Foo?page=2
Trace:
1
2

Hope this helps clarify some things.

这篇关于ASP.NET MVC的QueryString中重写默认提供的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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