使用URL.Action时出现路由问题

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

全部

我在控制器上定义了以下路由/操作:

I have the following route/action defined on my controller :

[RoutePrefix("widgets/download-functions")] [Route("download/{publishedReportId}"), HttpGet] public ActionResult Download(int publishedReportId)

在我的js代码中,我想使用适当的ID创建一些路由.

And inside my js code, I want to create some routing with the appropriate id.

这是我的js代码中包含的内容(不起作用).我想念什么?

This is what I have inside my js code (which doesn't work). What am I missing?

self.downloadFile = function (data) { console.log(data); console.log("@(Url.Action("Download", new { publishedReportId = 9999 }))"); console.log("@(Url.Action("Download"))"); };

对于这些东西和学习还是新手,我相信这很简单.

Still new to this stuff and learning, I am sure it is something simple.

运行此代码时,我将在控制台中看到以下内容

When I run this code, I get the following in the console

控制台输出

Thxjonpfl

Thx jonpfl

推荐答案

如果您使用的是Url.Action,它没有利用属性路由,则需要遵循原始的路由方法,即使用,操作,控制器,区域和参数,以获取用于生成网址的扩展方法.

If you are using Url.Action, it doesn't take advantage of attribute routing, you will need to follow the original method of routing, which is to use, Action, Controller, area and params to get the extension method to produce the url.

[RoutePrefix("widgets/download-functions")] public class WidgetDownloadController : Controller

...

[Route("download/{publishedReportId}"), HttpGet] public ActionResult Download(int publishedReportId)

您将获得一个类似Url.Action的操作

You would get a Url.Action like:

console.log("@(Url.Action("Download","WidgetDownload", new { publishedReportId = 9999 }))");

此外,这仅适用于由剃刀引擎(即)解析的脚本.直接在视图中编写的脚本.

Also, this will only work on scripts that are parsed by the razor engine, ie. scripts that are written directly in the view.

更多推荐

使用URL.Action时出现路由问题

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

发布评论

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

>www.elefans.com

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