MVC 5 Custom Route - Error 404 in LegacyRoute: RouteBase (example from the book Pro ASP.NET MVC 5 from Apress in Chapter 16)

Code presented in the book Pro ASP.NET MVC 5 from Apress in Chapter 16 (Inbound URL Routing). The example is for legacy URLs. Then I will deliver code samples for custom route, routing, controller and view.

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

      

I cannot figure out how it works. I always get 404 error ( http: //my.machine/articles/Windows_3.1_Overview.html ). The method in GetRouteData in LegacyRoute.cs is never called. If I delete. with url the code works fine. Can anyone provide advice or help?

It's strange that nobody complains that the code doesn't work.

+3


source to share


2 answers


Page 445 provides a solution. You need to edit your IIS Express settings.

Right click on the IIS Express icon in the taskbar while the application is running -> Show all applications -> Click on the site you want to configure -> Click on the config file

Search System.Web.Routing.UrlRoutingModule



Remove the value of the preCondition attribute so that the line becomes <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />

Reload the app and you're ready to go.

+2


source


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" });

      



Try with the above code, it should work.

Malinda Sanjaka

0


source







All Articles