mirror of
https://github.com/rudollee/LearningMVC.git
synced 2025-06-07 07:56:14 +00:00
Ch. 16 Routing Adv (until 16-21)
This commit is contained in:
parent
2fea7ffb5e
commit
59e0ccf99b
@ -5,6 +5,7 @@ using System.Web;
|
|||||||
using System.Web.Mvc;
|
using System.Web.Mvc;
|
||||||
using System.Web.Routing;
|
using System.Web.Routing;
|
||||||
using System.Web.Mvc.Routing.Constraints;
|
using System.Web.Mvc.Routing.Constraints;
|
||||||
|
using UrlsAndRoutes.Infrastructure;
|
||||||
|
|
||||||
namespace UrlsAndRoutes
|
namespace UrlsAndRoutes
|
||||||
{
|
{
|
||||||
@ -71,10 +72,16 @@ namespace UrlsAndRoutes
|
|||||||
|
|
||||||
routes.MapMvcAttributeRoutes();
|
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 });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,10 +17,16 @@ namespace UrlsAndRoutes.Controllers
|
|||||||
return View("ActionName");
|
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)
|
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)}")]
|
[Route("Add/{user}/{password:alpha:length(6)}")]
|
||||||
|
@ -22,5 +22,23 @@ namespace UrlsAndRoutes.Controllers
|
|||||||
ViewBag.CustomVariable = id; // ?? "<no value>"; // id; // RouteData.Values["id"];
|
ViewBag.CustomVariable = id; // ?? "<no value>"; // id; // RouteData.Values["id"];
|
||||||
return View();
|
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" });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
16
UrlsAndRoutes/Controllers/LegacyController.cs
Normal file
16
UrlsAndRoutes/Controllers/LegacyController.cs
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
47
UrlsAndRoutes/Infrastructure/LegacyRoute.cs
Normal file
47
UrlsAndRoutes/Infrastructure/LegacyRoute.cs
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -109,9 +109,11 @@
|
|||||||
<Compile Include="Controllers\AdminController.cs" />
|
<Compile Include="Controllers\AdminController.cs" />
|
||||||
<Compile Include="Controllers\CustomerController.cs" />
|
<Compile Include="Controllers\CustomerController.cs" />
|
||||||
<Compile Include="Controllers\HomeController.cs" />
|
<Compile Include="Controllers\HomeController.cs" />
|
||||||
|
<Compile Include="Controllers\LegacyController.cs" />
|
||||||
<Compile Include="Global.asax.cs">
|
<Compile Include="Global.asax.cs">
|
||||||
<DependentUpon>Global.asax</DependentUpon>
|
<DependentUpon>Global.asax</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<Compile Include="Infrastructure\LegacyRoute.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@ -119,6 +121,7 @@
|
|||||||
<Content Include="packages.config" />
|
<Content Include="packages.config" />
|
||||||
<Content Include="Views\Shared\ActionName.cshtml" />
|
<Content Include="Views\Shared\ActionName.cshtml" />
|
||||||
<Content Include="Views\Home\CustomVariable.cshtml" />
|
<Content Include="Views\Home\CustomVariable.cshtml" />
|
||||||
|
<Content Include="Views\Legacy\GetLegacyURL.cshtml" />
|
||||||
<None Include="Web.Debug.config">
|
<None Include="Web.Debug.config">
|
||||||
<DependentUpon>Web.config</DependentUpon>
|
<DependentUpon>Web.config</DependentUpon>
|
||||||
</None>
|
</None>
|
||||||
|
10
UrlsAndRoutes/Views/Legacy/GetLegacyURL.cshtml
Normal file
10
UrlsAndRoutes/Views/Legacy/GetLegacyURL.cshtml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
@model string
|
||||||
|
@{
|
||||||
|
ViewBag.Title = "GetLegacyURL";
|
||||||
|
Layout = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
<h2>GetLegacyURL</h2>
|
||||||
|
|
||||||
|
The URL requested was: @Model
|
||||||
|
|
@ -12,11 +12,25 @@
|
|||||||
<body>
|
<body>
|
||||||
<div>The controller is: @ViewBag.Controller</div>
|
<div>The controller is: @ViewBag.Controller</div>
|
||||||
<div>The action is: @ViewBag.Action</div>
|
<div>The action is: @ViewBag.Action</div>
|
||||||
<div>
|
@*<div>
|
||||||
@Html.ActionLink("This is an outgoing URL", "CustomVariable")
|
@Html.ActionLink("This is an outgoing URL", "CustomVariable", new { id = "Hello" })
|
||||||
</div>
|
</div>*@
|
||||||
<div>
|
@*<div>
|
||||||
@Html.ActionLink("This targets another controller", "Index", "Customer")
|
@Html.ActionLink("This targets another controller", "Index", "Customer")
|
||||||
|
</div>*@
|
||||||
|
<div>
|
||||||
|
<!-- Add Attribute-->
|
||||||
|
@* @Html.ActionLink("This is an outgoing URL", "Index", "Home", null, new { id = "myAnchorID", @class = "myCSSClass" })*@
|
||||||
|
|
||||||
|
<!-- Regular Expression-->
|
||||||
|
@*@Html.ActionLink("This is an outgoing URL", "Index", "Home", "https", "myserver.mydomain.com", "myFragmentName",
|
||||||
|
new { id = "MyId" },
|
||||||
|
new { id = "myAnchorID", @class = "myCSSClass" })*@
|
||||||
|
|
||||||
|
<!-- Show URL without html-->
|
||||||
|
This is a URL:
|
||||||
|
@*@Url.Action("Index", "Home", new { id = "MyId" })*@
|
||||||
|
@Html.ActionLink("Click me", "GetLegacyURL", new { legacyURL = "~/articls/Windows_3.1_Overview.html" })
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user