Apress第16章(路由传入URL)中的Pro ASP.NET MVC 5一书中提供的代码该示例与遗留URL有关。接下来,我将为自定义路由,路由配置,控制器和视图提供代码示例。
LegacyRoute.cs
using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace UrlsAndRoutes.Infrastructure
{
public class LegacyRoute : RouteBase
{
private string[] urls;
public LegacyRoute(params string[] targetUrls)
{
urls = targetUrls;
}
public override RouteData GetRouteData(HttpContextBase httpContext)
{
RouteData result = null;
string requestedURL = httpContext.Request.AppRelativeCurrentExecutionFilePath;
if (urls.Contains(requestedURL, StringComparer.OrdinalIgnoreCase))
{
result = new RouteData(this, new MvcRouteHandler());
result.Values.Add("controller", "Legacy");
result.Values.Add("action", "GetLegacyURL");
result.Values.Add("legacyURL", requestedURL);
}
return result;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
VirtualPathData result = null;
if (values.ContainsKey("legacyURL") && urls.Contains((string)values["legacyURL"], StringComparer.OrdinalIgnoreCase))
{
result = new VirtualPathData(this, new UrlHelper(requestContext).Content((string)values["legacyURL"]).Substring(1));
}
return result;
}
}
}
RouteConfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Mvc.Routing.Constraints;
using UrlsAndRoutes.Infrastructure;
namespace UrlsAndRoutes
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapMvcAttributeRoutes();
//routes.MapRoute("NewRoute", "App/Do{action}", new { controller = "Home", id = UrlParameter.Optional });
//routes.Add(new Route("SayHello", new CustomRouteHandler()));
routes.Add(new LegacyRoute("~/articles/Windows_3.1_Overview.html", "~/old/.NET_1.0_Class_Library"));
routes.MapRoute("MyRoute", "{controller}/{action}", new { controller = "Home", action = "Index" });
routes.MapRoute("MyOtherRoute", "App/{action}", new { controller = "Home" });
}
}
}
LegacyController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace UrlsAndRoutes.Controllers
{
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
我无法想象如何运作。我总是得到404错误(http://my.machine/articles/Windows_3.1_Overview.html)。永远不会调用LegacyRoute.cs中GetRouteData中的方法。如果我删除了。从URL,代码工作正常。任何人都可以提供一些建议或帮助吗?
奇怪的是,没有人抱怨代码不起作用。
答案 0 :(得分:2)
第445页提供了解决方案。您需要编辑IIS Express设置。
在应用程序运行时右键单击任务栏上的IIS Express图标 - &gt;显示所有应用程序 - &gt;点击您要配置的网站 - &gt;单击配置文件
搜索System.Web.Routing.UrlRoutingModule
删除preCondition属性值,使该行变为<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
重新启动应用程序,您已做好准备。
答案 1 :(得分:0)
routes.MapMvcAttributeRoutes();
routes.Add(new LegacyRoute("~/articles/Windows_3.1_Overview.html/",
"~/old/.NET_1.0_Class_Library/"));
routes.MapRoute("MyRoute", "{controller}/{action}", new { controller = "Home" ,action="index" });
routes.MapRoute("MyOtherRoute", "App/{action}", new { controller = "Home" });
尝试使用上面的代码,这应该可以使用。
Malinda Sanjaka