繁体   English   中英

@Url.Action 当前 url 的 pass id 段

[英]@Url.Action pass id segment of current url

我正在 dotnet core 3.1 C# MVC 中开发应用程序。

我在 Startup.cs 文件中有 URL 映射,如下所示:

app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Account}/{action=Index}/{id?}");
            });

有一个名为 Dashboard 的 controller 和操作方法索引,我有这样的 URL。

<domain>/<controller>/<action>/<id>
http://localhost:57088/Dashboard/Index/03359edbae9543b5a1bc956e9282cfb6

在 Razor 页面中,每当我使用 @Url.Action 创建指向相同 controller 和相同操作的链接时,它会自动添加 ZE6B391A8D2C4D45902A23A8B6585703D 的 id 段。

@Url.Action("Index", "Dashboard");

然后它像这样创建 URL:

http://localhost:57088/Dashboard/Index/03359edbae9543b5a1bc956e9282cfb6

但是,当我使用 @Url.Action 创建 URL 到不同的 controller 时,它不会添加 URL 的 id 段。

@Url.Action("DoSomething", "Photos");

然后它像这样创建 URL:

http://localhost:57088/Photos/DoSomething

但我怎样才能像这样创建 URL:

http://localhost:57088/Photos/DoSomething/03359edbae9543b5a1bc956e9282cfb6

如何使用@Url.Action 将相同的 id 段添加到不同的 controller?

IUrlHelper.Action一个重载,它接受包含路由值的匿名 object。 您需要调用此重载并传递必要的信息以正确识别您的操作。

例如,假设您的 Id 存储在 Model 上的Guid字段中:

public class MyModel
{
   public Guid Id { get; set; }
   // other properties
}

Url.Action的调用可能如下所示:

@Url.Action("DoSomething", "Photos", new { id = Model.Id });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM