简体   繁体   English

如何在 URL MVC 路由上从问号 (?) 更改为斜线 (/)?

[英]How do I change from question mark (?) to slash (/) on URL MVC Route?

Currently I have pdf file that shows on the page correctly but the URL shows as "/Test?id=name.pdf" and I want it to show as "/Test/name.pdf".目前我有正确显示在页面上的 pdf 文件,但 URL 显示为“/Test?id=name.pdf”,我希望它显示为“/Test/name.pdf”。 How can I achieve this?我怎样才能做到这一点?

Here is my code:这是我的代码:

.cshtml .cshtml

<a class="dropdown-item" href="@Url.Action("Trees", "Park", new { id = "name.pdf" })" target="_blank">Text</a>

Controller控制器

public ActionResult Trees(string id)
{
    string path = Server.MapPath(String.Format("~/folder1/folder2/{0}", id));

    string mime = MimeMapping.GetMimeMapping(path);

    return File(path, mime);
}

RouteConfig.cs路由配置文件

    routes.MapRoute(
        name: "testing",
        url: "Test/{filename}",
        defaults: new { controller = "Park", action = "Trees", filename  = UrlParameter.Optional }
    );

Any idea what I'm doing wrong?知道我做错了什么吗? I already checked these similar questions ASP.NET MVC Routing question How do I get rid of the question mark in an ASP.NET MVC route?我已经检查了这些类似的问题ASP.NET MVC 路由问题如何摆脱ASP.NET MVC 路由中的问号? but none of those solutions helped me, maybe is cause I have a @Url.Action with new { id = "name.pdf" } and that could be the difference since none of those questions had that.但这些解决方案都没有帮助我,也许是因为我有一个 @Url.Action 和 new { id = "name.pdf" } 这可能是不同的,因为这些问题都没有。

Any suggestions is appreciated.任何建议表示赞赏。 Thanks.谢谢。

Your route url parameter is Test/{filename} hence filename should replace id parameter in your cshtml and controller.您的路由 url 参数是Test/{filename}因此filename应该替换您的 cshtml 和控制器中的id参数。

EDIT: You need to add your route above the default route and add a parameter to webconfig module.编辑:您需要将您的路由添加到默认路由之上,并向 webconfig 模块添加一个参数。 We also need to edit the webconfig because by default they don't support the .我们还需要编辑 webconfig,因为默认情况下它们不支持. symbol.象征。

EDIT 2: Don't forget to add the directory or file to Visual studio solution.编辑 2:不要忘记将目录或文件添加到 Visual Studio 解决方案。

routeconfig.cs: Add the route before the default route. routeconfig.cs:在默认路由之前添加路由。

public static void RegisterRoutes(RouteCollection routes)
{
   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

   routes.MapRoute(
      name: "testing",
      url: "Test/{filename}",
      defaults: new { controller = "Park", action = "Trees", filename = UrlParameter.Optional }
   );

   routes.MapRoute(
      name: "Default",
      url: "{controller}/{action}/{id}",
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
   );
}

webconfig: Add the parameter runAllManagedModulesForAllRequests="true" to modules node. webconfig:添加参数runAllManagedModulesForAllRequests="true"到 modules 节点。

<system.webserver>
   <modules runAllManagedModulesForAllRequests="true">
      ...
   </modules>
</system.webserver>

cshtml: cshtml:

<a class="dropdown-item" href="@Url.Action("Trees", "Park", new { @filename = "name.pdf" })" target="_blank">Text</a>

controller:控制器:

public ActionResult Trees(string filename)
{
    string path = Server.MapPath(String.Format("~/folder1/folder2/{0}", filename));

    string mime = MimeMapping.GetMimeMapping(path);

    return File(path, mime);
}

I finally found the solution!我终于找到了解决方案!

So it came down to two things: eliminating the id = UrlParameter.Optional on RouteConfig.cs which causes the question mark (?), AND understanding that apparently you can't put a dot (.) inside the brackets {}, because if you try to do so, then the brackets {} won't accept the input and thus the path would be incorrect.所以它归结为两件事:消除id = UrlParameter.Optional上导致问号 (?) 的id = UrlParameter.Optional ,并且理解显然你不能在方括号 {} 内放一个点 (.),因为如果如果您尝试这样做,则括号 {} 将不接受输入,因此路径将不正确。

So, here is how the code looks updated:所以,这里是代码的更新方式:

.cshtml .cshtml

//Took out .pdf so that the brackets on the controller can read the input
<a class="dropdown-item" href="@Url.Action("Trees", "Park", new { filename = "name" })" target="_blank">Text</a>

Controller:控制器:

public ActionResult Trees(string filename)
{
    //Added .pdf after the {}, ideally this wouldn't be hard coded but that's out of
    //the scope of this question/problem
    string path = Server.MapPath(String.Format("~/folder1/folder2/{0}.pdf", filename));

    string mime = MimeMapping.GetMimeMapping(path);

    return File(path, mime);
}

RouteConfig.cs: RouteConfig.cs:

    routes.MapRoute(
        name: "testing",
        url: "Test/{filename}",
        defaults: new { controller = "Park", action = "Trees"}
        //Remove the URLParameter.Optional
    );

This results in the PDF opening successfully AND the URL to be Test/name with the slash (/) and not the question mark (?).这会导致 PDF 成功打开,并且 URL 是带有斜杠 (/) 而不是问号 (?) 的测试/名称。 The only thing to 'fix' is for the URL to also show the extension (.pdf), but that's for another question in another thread.唯一需要“修复”的是 URL 还显示扩展名 (.pdf),但这是另一个线程中的另一个问题。 But for replacing the question mark (?) with the slashes (/) AND opening the PDF successfully, this solution achieves that and answers my original question.但是为了用斜杠 (/) 替换问号 (?) 并成功打开 PDF,此解决方案实现了这一点并回答了我原来的问题。

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

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