使用URL.Action时出现路由问题 [英] Routing issue when using URL.Action

查看:94
本文介绍了使用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时出现路由问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆