在网址

时间:2018-05-15 02:11:45

标签: asp.net-mvc asp.net-mvc-5

我在RouteConfig.cs中有以下路线:

routes.MapRoute(
    "MyLegacyRoute",
    "Content/bootstrap.css",
    new { controller = "Legacy", action = "GetLegacyUrl", legacyUrl = "someUrl" });

~/Content/bootstrap.css存在,而不是在我导航到http://localhost:27541/Content/bootstrap.css时显示其内容,我想点击GetLegacyUrl控制器中的Legacy操作。

我已将此添加到Web.config:

<system.webServer>
    <handlers>
      <add name="UrlRoutingHandler" path="/Content/*" verb="GET" type="System.Web.Routing.UrlRoutingHandler" />
...

<system.web>
<httpRuntime targetFramework="4.5" relaxedUrlToFileSystemMapping="true"/>
...

但是,当我访问http://localhost:27541/Content/bootstrap.css时,我收到此错误:

  

&#39; /&#39;中的服务器错误应用。
  无法创建抽象类   描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的更多信息。

     

异常详细信息:System.MissingMethodException:无法创建抽象类。

我的控制器看起来像这样:

public class LegacyController : Controller
{
    public ActionResult GetLegacyUrl(string legacyUrl)
    {
        return View((object)legacyUrl);
    }
}

我的观点(GetLegacyUrl.cshtml)如下所示:

@model string
@{
    ViewBag.Title = "GetLegacyUrl";
    Layout = null;
}

<h2>GetLegacyUrl</h2>
The URL requested was @Model

我正在尝试这样做,以便我可以了解有关路由的更多信息。

如何成功使用此路线?

1 个答案:

答案 0 :(得分:0)

我把它放在RouteConfig.cs

routes.RouteExistingFiles = true;

preCondition值设为空字符串:

<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />

在此文件中:C:\Users\user.name\Documents\IISExpress\config\applicationhost.config

我在Adam Freeman的Pro ASP.NET MVC5书中找到了第99/115页第16章的答案。

我还删除了我在Web.config中添加的处理程序并删除了relaxedUrlToFileSystemMapping="true",因为它在MVC5中不起作用。

相关问题