URL.Action(),包括路由值

编程入门 行业动态 更新时间:2024-10-25 04:17:03
本文介绍了URL.Action(),包括路由值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个ASP.Net MVC 4应用程序,现在用的是Url.Action助手这样的: @ Url.Action(信息,管理)

本页面同时用于增加新的编辑和管理员的配置文件。网址如下:

添加新:HTTP://本地主机:4935 /管理/信息 编辑现有:HTTP://本地主机:4935 /管理/信息/ 5℃==管理员ID

当我在网站的编辑现有部分,并决定我想补充一个新的管理员我点击以下链接:

< A HREF =@ Url.Action(信息,管理员)>添加一个管理员< / A>

但问题是,上面的链接实际上是将的http://本地主机:4935 /管理/信息/ 5 。这只有当我在该页面编辑现有的管理是发生了。在网站上其他任何地方都可以正确链接到的http://本地主机:4935 /管理/信息

有其他人看到这一点?

更新:

RouteConfig:

routes.MapRoute(            名称:默认,            网址:{控制器} / {行动} / {ID}            默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}        );

解决方案

在MVC传出URL生成的基于当前的路由架构。

由于您的信息的操作方法需要id参数,和你的路由集合了当前的请求的URL的ID(/管理/信息/ 5),从现有的路由集合值自动得到。id参数

要解决这个问题,你应该使用UrlParameter.Optional:

< A HREF =@ Url.Action(信息,管理,新{ID = UrlParameter.Optional})>添加一个管理员< / A>

I have an ASP.Net MVC 4 app and am using the Url.Action helper like this: @Url.Action("Information", "Admin")

This page is used for both adding a new and edit an admin profile. The URLs are as follows:

Adding a new: localhost:4935/Admin/Information Editing Existing: localhost:4935/Admin/Information/5 <==Admin ID

When I'm in the Editing Existing section of the site and decide that I would like to add a new admin I click on the following link:

<a href="@Url.Action("Information", "Admin")">Add an Admin</a>

The problem however that the above link is actually going to localhost:4935/Admin/Information/5. This only happens when I'm in that page editing an existing admin. Anywhere else on the site it links correctly to localhost:4935/Admin/Information

Has anyone else seen this?

UPDATE:

RouteConfig:

routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );

解决方案

outgoing url in mvc generated based on the current routing schema.

because your Information action method require id parameter, and your route collection has id of your current requested url(/Admin/Information/5), id parameter automatically gotten from existing route collection values.

to solve this problem you should use UrlParameter.Optional:

<a href="@Url.Action("Information", "Admin", new { id = UrlParameter.Optional })">Add an Admin</a>

更多推荐

URL.Action(),包括路由值

本文发布于:2023-11-03 20:37:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556032.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   URL   Action

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!