ASP.NET MVC @ Url.Action包含当前路由数据 [英] ASP.NET MVC @Url.Action includes current route data

查看:48
本文介绍了ASP.NET MVC @ Url.Action包含当前路由数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我:

  • 在我的 Global.asax 文件中具有路由 {controller}/{action}/{id} .
  • 控制器 Foo 和操作 Bar(String id)返回视图.
  • 一个非常简单的视图,其中包含由 @ Url.Action("bar","foo")呈现的URL-明确地未指定 id .
  • 如果我浏览到/foo/bar/test ,该视图将显示呈现的URL为/foo/bar/test .我本来希望它应该是/foo/bar ,因为我没有为 id 指定值.
  • Have a route {controller}/{action}/{id} in my Global.asax file.
  • A Controller Foo and Action Bar(String id) returning a view.
  • A very simple View containing a URL rendered by @Url.Action("bar", "foo") - explicitly NOT specifying an id.
  • If I browse to /foo/bar/test, the view will show the rendered URL as /foo/bar/test. I would have expected that it should be /foo/bar as I didn't specify a value for id.

为什么即使我没有指定它也包含当前ID,并且有什么方法可以防止这种情况(除了直接对URL进行硬编码"之外)?

Why would it include the current id even when I didn't specify it, and is there any way of preventing this (apart from 'hard-coding' the URL directly)?

推荐答案

在浏览器地址栏中输入/foo/bar/test 时,您确实指定了id的值.您实际上是在foo控制器中使用参数"test"调用方法 Bar .直接浏览时,您的简单视图无关紧要.

You did specify the value for id when you put /foo/bar/test in the browser address bar. You are essentially calling the method Bar with the parameter "test" in your foo controller. Your simple view is irrelevant when you browse directly.

要回答最后一个问题,您可能需要重新考虑使用MVC的方式.

To answer your last question you may need to have a re-look at the way you are using MVC.

这篇关于ASP.NET MVC @ Url.Action包含当前路由数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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