0

我在使用带有 url 格式的 iModelBinder 时遇到问题

http://localhost/controller/action/id/value

动作将是控制器中的函数,即 id/value 是 ie。id=12

当我尝试上面的链接时,我收到一个 404 错误页面未找到,并且查看堆栈我可以理解 MVC 正在寻找它不理解的路径。

使用以下作品

http://localhost/controller/action?id=value

如果有人知道是否可以解决此问题,我真的很希望能够使用“/”作为分隔符。

文斯

4

1 回答 1

1

url 应该真正采用以下格式:

http://localhost/controller/action/id

例如:

http://localhost/products/index/1

然后应该在控制器操作中指定 id。例如:

public ActionResult Index(int id)
{
    ...

global.asax 文件中指定的路由将指定 url 的格式。对于上面的 url,默认路由就足够了:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

然后默认的模型绑定器会自动将你的 id(即上面 url 中的 1)绑定到 action 中的 int id。

就像亚当建议的那样,我认为您不应该在 url 中指定 id 的名称,因为默认模型绑定器会自动为您绑定。

于 2012-02-09T07:14:59.767 回答