簡體   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