diff --git a/UrlsAndRoutes/App_Start/RouteConfig.cs b/UrlsAndRoutes/App_Start/RouteConfig.cs index 0e28295..6430c53 100644 --- a/UrlsAndRoutes/App_Start/RouteConfig.cs +++ b/UrlsAndRoutes/App_Start/RouteConfig.cs @@ -5,6 +5,7 @@ using System.Web; using System.Web.Mvc; using System.Web.Routing; using System.Web.Mvc.Routing.Constraints; +using UrlsAndRoutes.Infrastructure; namespace UrlsAndRoutes { @@ -71,10 +72,16 @@ namespace UrlsAndRoutes routes.MapMvcAttributeRoutes(); - routes.MapRoute("NewRoute", "App/Do{action}", new { controller = "Home" }); + //routes.MapRoute("NewRoute", "App/Do{action}", new { controller = "Home" }); + + //routes.MapRoute("MyRoute", "{controller}/{action}/{id}", + // new { controller = "Home", action = "Index", id = UrlParameter.Optional }); + + routes.Add(new LegacyRoute("~/articles/Windows_3.1_Overview.html", "~/old/.NET_1.0_Class_Library")); + + routes.MapRoute("MyRoute", "{controller}/{action}"); + routes.MapRoute("MyOtherRoute", "App/{action}", new { controller = "Home" }); - routes.MapRoute("MyRoute", "{controller}/{action}/{id}", - new { controller = "Home", action = "Index", id = UrlParameter.Optional }); } } } diff --git a/UrlsAndRoutes/Controllers/CustomerController.cs b/UrlsAndRoutes/Controllers/CustomerController.cs index 0ccf169..8d29e49 100644 --- a/UrlsAndRoutes/Controllers/CustomerController.cs +++ b/UrlsAndRoutes/Controllers/CustomerController.cs @@ -17,10 +17,16 @@ namespace UrlsAndRoutes.Controllers return View("ActionName"); } - [Route("Add/{user}/{id:int}")] + //[Route("Add/{user}/{id:int}")] + //public string Create(string user, int id) + //{ + // return string.Format("User: {0}, ID: {1}", user, id); + //} + + [Route("Add/{user}/{id:int}", Name = "AddRoute")] public string Create(string user, int id) { - return string.Format("User: {0}, ID: {1}", user, id); + return string.Format("Create Method - User: {0}, ID: {1}", user, id); } [Route("Add/{user}/{password:alpha:length(6)}")] diff --git a/UrlsAndRoutes/Controllers/HomeController.cs b/UrlsAndRoutes/Controllers/HomeController.cs index a224e01..3fe9007 100644 --- a/UrlsAndRoutes/Controllers/HomeController.cs +++ b/UrlsAndRoutes/Controllers/HomeController.cs @@ -22,5 +22,23 @@ namespace UrlsAndRoutes.Controllers ViewBag.CustomVariable = id; // ?? ""; // id; // RouteData.Values["id"]; return View(); } + + //public ViewResult MyActionMethod() + //{ + // string myActionUrl = Url.Action("Index", new { id = "MyID" }); + // string myRouteUrl = Url.RouteUrl(new { controller = "Home", action = "Index" }); + + // return View(); + //} + + //public RedirectToRouteResult MyActionMethod() + //{ + // return RedirectToAction("Index"); + //} + + public RedirectToRouteResult MyActionMethod() + { + return RedirectToRoute(new { controller = "Home", action = "Index", id = "MyID" }); + } } } \ No newline at end of file diff --git a/UrlsAndRoutes/Controllers/LegacyController.cs b/UrlsAndRoutes/Controllers/LegacyController.cs new file mode 100644 index 0000000..b5ca3dc --- /dev/null +++ b/UrlsAndRoutes/Controllers/LegacyController.cs @@ -0,0 +1,16 @@ +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); + } + } +} \ No newline at end of file diff --git a/UrlsAndRoutes/Infrastructure/LegacyRoute.cs b/UrlsAndRoutes/Infrastructure/LegacyRoute.cs new file mode 100644 index 0000000..bc32c40 --- /dev/null +++ b/UrlsAndRoutes/Infrastructure/LegacyRoute.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +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; + } + } +} \ No newline at end of file diff --git a/UrlsAndRoutes/UrlsAndRoutes.csproj b/UrlsAndRoutes/UrlsAndRoutes.csproj index 22ce40c..473b0b2 100644 --- a/UrlsAndRoutes/UrlsAndRoutes.csproj +++ b/UrlsAndRoutes/UrlsAndRoutes.csproj @@ -109,9 +109,11 @@ + Global.asax + @@ -119,6 +121,7 @@ + Web.config diff --git a/UrlsAndRoutes/Views/Legacy/GetLegacyURL.cshtml b/UrlsAndRoutes/Views/Legacy/GetLegacyURL.cshtml new file mode 100644 index 0000000..d5a841e --- /dev/null +++ b/UrlsAndRoutes/Views/Legacy/GetLegacyURL.cshtml @@ -0,0 +1,10 @@ +@model string +@{ + ViewBag.Title = "GetLegacyURL"; + Layout = null; +} + +

GetLegacyURL

+ +The URL requested was: @Model + diff --git a/UrlsAndRoutes/Views/Shared/ActionName.cshtml b/UrlsAndRoutes/Views/Shared/ActionName.cshtml index 5cf8af0..0e32cc5 100644 --- a/UrlsAndRoutes/Views/Shared/ActionName.cshtml +++ b/UrlsAndRoutes/Views/Shared/ActionName.cshtml @@ -12,11 +12,25 @@
The controller is: @ViewBag.Controller
The action is: @ViewBag.Action
-
- @Html.ActionLink("This is an outgoing URL", "CustomVariable") -
-
+ @*
+ @Html.ActionLink("This is an outgoing URL", "CustomVariable", new { id = "Hello" }) +
*@ + @*
@Html.ActionLink("This targets another controller", "Index", "Customer") +
*@ +
+ +@* @Html.ActionLink("This is an outgoing URL", "Index", "Home", null, new { id = "myAnchorID", @class = "myCSSClass" })*@ + + + @*@Html.ActionLink("This is an outgoing URL", "Index", "Home", "https", "myserver.mydomain.com", "myFragmentName", + new { id = "MyId" }, + new { id = "myAnchorID", @class = "myCSSClass" })*@ + + + This is a URL: + @*@Url.Action("Index", "Home", new { id = "MyId" })*@ + @Html.ActionLink("Click me", "GetLegacyURL", new { legacyURL = "~/articls/Windows_3.1_Overview.html" })