追加?参数= MVC的路线 [英] Appending ?param= to mvc routes

查看:81
本文介绍了追加?参数= MVC的路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些MVC网站也查询字符串PARAMS附加到路径网址(其中我注意到StackOverflow上一样),如:

Some MVC sites have querystring params appended to the route Url (of which I noticed StackOverflow does), such as:

<一个href=\"http://stackoverflow.com/questions/tagged/java\">http://stackoverflow.com/questions/tagged/java?page=9802&sort=newest&pagesize=15

什么是具有参数更常规?查询字符串则params的优点,而不是/参数/值/?

What are the advantages of having the parameters as more conventional ?querystring params, rather than /param/values/ ?

另外,如何在这些PARAMS附加到已设置的路线?我熟悉像用户/细节/(编号)等PARAMS建立MVC的路线,但不知道如何配置使用路由与1个或更多?PARAMS按照上面的例子网址是什么?

Also, how are these params appended to routes that have been set up? I'm familiar with setting up mvc routes with params like "users/details/{id}" etc. but don't know how to configure routes for use with 1 or more ?params as per the example url above?

推荐答案

当你有多个可选参数,并且不希望包括默认值,非指定的参数只是为了满足路径查询字符串参数都是有用的。

Query string parameters are useful when you have multiple optional parameters and don't want to include default values for non-specified parameters just to satisfy a path.

和你没有做什么特别包括在渲染URL这些参数。

And you don't have to do anything special to include these parameters in a rendered URL.

以下面的路径,例如:

routes.MapRoute
(
    "QuestionsTagged",
    "questions/tagged/{tag}",
    new { controller = "Questions", action = "Tagged" }
);

如果您在使用呈现一个链接到该路线:

If you render a link to that route using:

Url.RouteUrl
(
    "QuestionsTagged",
    new
    {
        tag = "java",
        page = 9802,
        sort = "newest",
        pagesize = 15
    }
 )

...那么路由引擎足够聪明地看到,路线包含一个名为参数标记并通过路径值也反对有事名为标记因此它使用的路由值。

...then the routing engine is smart enough to see that the route contains a parameter named tag and that the passed route values object also has something named tag so it uses that value in the route.

提供任何路由值是的有在路由相应的参数(排序页面大小在这种情况下)获得上涨了作为查询字符串参数。因此, Url.RouteUrl 拨打以上将返回 /问题/标记/ Java页面= 9802&放大器;排序=最新和放大器;页面大小= 15

Any provided route values that don't have corresponding parameters in the route (page, sort and pagesize in this case) get tacked on as query string parameters. So the Url.RouteUrl call above would return /questions/tagged/java?page=9802&sort=newest&pagesize=15.

和动作方法可以明确地在其签名列出这些参数(促进可读性和可维护性),或者你可以通过的Request.QueryString 访问它们。

And your action method can explicitly list these parameters in its signature (promotes readability and maintainability) or you can access them via Request.QueryString.

public class QuestionsController : Controller
{
    // I can explicitly list the parameters in my signature and let routing do
    // its magic, like this...
    public ViewResult Tagged(string tag, int? page, int? pagesize)
    {
        // ...or I can grab parameters like this:
        string sort = Request.QueryString["sort"];

        return View();
    }
}

请注意,该参数到操作方法不必匹配路由指定的参数。 (在路线,我只指定标记,但是动作方法的签名名单标记页面大小)。但是,操作方法的任何参数,是不是也该路由的参数必须是引用或可空类型

Note that the parameters to the action method do not have to match the parameters specified in the route. (In the route, I only specified tag, but the action method's signature lists tag, page, and pagesize.) However, any parameter of the action method that is not also a parameter of the route must be a reference or nullable type.

这篇关于追加?参数= MVC的路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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