Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为具有属性路由的操作创建URL_C#_Asp.net Mvc_Asp.net Mvc 5_Asp.net Mvc Routing_Attributerouting - Fatal编程技术网

C# 为具有属性路由的操作创建URL

C# 为具有属性路由的操作创建URL,c#,asp.net-mvc,asp.net-mvc-5,asp.net-mvc-routing,attributerouting,C#,Asp.net Mvc,Asp.net Mvc 5,Asp.net Mvc Routing,Attributerouting,我在具有ASP.NET MVC 5属性路由的控制器中执行了操作 public class HomeController : BaseController { [Route("{srcFileFormat}-to-{dstFileFormat}")] public ActionResult Converter(string srcFileFormat, string dstFileFormat) { } } 我正在尝试创建url,并且总是得到Null而不是url。在我的例子中,有

我在具有ASP.NET MVC 5属性路由的控制器中执行了操作

public class HomeController : BaseController
{
  [Route("{srcFileFormat}-to-{dstFileFormat}")]
  public ActionResult Converter(string srcFileFormat, string dstFileFormat)
  {
  }
}
我正在尝试创建url,并且总是得到Null而不是url。在我的例子中,有没有关于如何同时使用
UrlHelper.Action
属性路由
的建议

@Url.Action("docx-to-pdf", "Home")

Url.Action
中,我们指定控制器名称和操作方法名称,而不是您尝试的路由命名或参数

我们通常通过以下方式获取控制器操作的url:

@Url.Action("Converter", "Home")
但是,由于您的操作方法也需要两个参数,并且您正在尝试传入这些参数,因此您也需要调用它来传递参数,如:

@Url.Action("Converter", "Home", new {srcFileFormat ="doc",dstFileFormat="pdf"})
现在,它应该生成如下url:

localhost:6087/doc-to-pdf