ASP.NET MVC 路由查询字符串与嵌入值的困惑
这可能已经被问过 - 抱歉!我找不到它。 我不确定 asp 如何决定何时使用查询字符串和“正常路径”(嵌入值) 以此为例:
routes.MapRoute(
"SomePage",
"Net/Fix/{value}",
new { controller = "Net", action = "Index" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
我不知道如何解释 - 我会尝试..如果我错了,请解释
问题1。 mapRoute 中的第一个参数是为了让我们可以指定使用超链接时要进行的路由吗?
问题2。 第二个参数有什么作用? 看起来好像第二个参数为您提供了指定路由应如何发生的选项,如下所示:“Net/Fix/hello”或通过以 {controller}/{action}/{somevar} 形式指定占位符
问题 3: 我假设如果问题 2 场景中没有使用任何内容 - 这指定了应该发生的默认路由?
问题4: ASP.NET 如何推断是使用查询字符串还是嵌入值..
因为例如当我决定调用我的页面时
http:/localhost:portno/Net/Fix/hello
它会尽职尽责地打印 hello.. 但是当我执行 this 时
http:/localhost:portno/Net/Index/hello
它不会工作..除非我这样做
http:/localhost:portno/Net/Index?value=hello..
问题是...为什么?!!!
我希望问题很清楚..我会回复答案(如果稍后有的话)。
This has probably been asked already - if so sorry! I couldn't find it.
I am unsure as to how asp is able to decide when to use a query string and "normal looking paths" (Embedded values)
Take this for example:
routes.MapRoute(
"SomePage",
"Net/Fix/{value}",
new { controller = "Net", action = "Index" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I don't know how to explain - I will try.. If I am wrong please explain it
Question 1.
Is the first argument in mapRoute so that we can specify which routing we want to take place when using hyperlinks?
Question 2.
What does the second argument do?
It appears as if the second argument gives you the option of specifying how the routing should occur as below: "Net/Fix/hello" or by specifying placeholders in the form of {controller}/{action}/{somevar}
Question 3:
I assume if nothing is used in question 2 scenario - this specifies default routing that should take place?
Question 4:
How does ASP.NET infer whether to use a query string or an embedded value..
Because for example when I decide to call my page
http:/localhost:portno/Net/Fix/hello
It dutifully prints hello.. but when I do this
http:/localhost:portno/Net/Index/hello
It doesn't work.. unless I do
http:/localhost:portno/Net/Index?value=hello..
Question is... why?!!!
I hope questions were clear.. I will reply to answers (if any later).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

第一个参数是路由名称。每个路由应该有一个唯一的名称,它们可用于创建链接,以确保链接基于特定路由。对于您匹配路线的情况来说,这并不重要。
第二个参数是匹配模式。文字值以清晰的形式显示,参数化值位于大括号内。 <代码>{}。参数化值不仅用于指定参数的位置,还用于指定参数的名称。
我立即不确定为什么您会定义没有任何匹配模式的路线。 MapRoute() 的这种重载是否存在?
您获得使用此网址执行的行为的原因:
http:/localhost:portno/Net/Index?value=hello
它与第二(默认)路由匹配,不是第一个。但是,看看第二个路由模式:
控制器是第一个参数,操作是第二个参数。因此,通过您的 URL,该请求将路由到
Net
控制器的Index
操作。与你的第一个例子相同。由于查询字符串包含
value
参数,因此该参数仍会传递给操作方法。碰巧您的操作方法有一个名为value
的string
参数,因此它可以工作。The first argument is a route name. Each route should have a unique name, and they can be used for creating links, to assure a link is based on a certain route. It's not important in your case of matching a route.
The second argument is a matching pattern. Literal values are shown in clear, and parameterized values inside curly braces.
{}
. The parameterized values are not just for specifying the location of a parameter, but also the name of it.I'm not sure offhand why you would define a route without any matching pattern. Does such an overload of
MapRoute()
exist?The reason you get the behavior you do with this url:
http:/localhost:portno/Net/Index?value=hello
It matches the second (the default) route, not the first.However, look at the second route pattern:
The controller is the first parameter, action is the second. So with your URL, that request is routed to the
Net
controller,Index
action. the same as your first example.Because the query string contains a
value
parameter, that still gets passed to the action method. And it just so happens your action method has astring
parameter namedvalue
, so it works.