code_junkie

Friendly URLs for ASP.NET

Nov 10th, 2011
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.62 KB | None | 0 0
  1. <system.web>
  2. <compilation debug="true">
  3. <assemblies>
  4. …
  5. <add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  6. </assemblies>
  7. </compilation>
  8. …
  9. <httpModules>
  10. …
  11. <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  12. </httpModules>
  13. </system.web>
  14. <system.webServer>
  15. …
  16. <modules>
  17. …
  18. <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  19. </modules>
  20. <handlers
  21. …
  22. <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
  23. </handlers>
  24. </system.webServer>
  25.  
  26. void Application_Start(object sender, EventArgs e)
  27. {
  28. RegisterRoutes(RouteTable.Routes);
  29. }
  30.  
  31. public static void RegisterRoutes(RouteCollection routes)
  32. {
  33. routes.Add("UseridRoute", new Route
  34. (
  35. "userid/{userid}",
  36. new CustomRouteHandler("~/users.aspx")
  37. ));
  38. }
  39.  
  40. using System.Web.Compilation;
  41. using System.Web.UI;
  42. using System.Web;
  43. using System.Web.Routing;
  44.  
  45. public class CustomRouteHandler : IRouteHandler
  46. {
  47. public CustomRouteHandler(string virtualPath)
  48. {
  49. this.VirtualPath = virtualPath;
  50. }
  51.  
  52. public string VirtualPath { get; private set; }
  53.  
  54. public IHttpHandler GetHttpHandler(RequestContext
  55. requestContext)
  56. {
  57. // Add the querystring to the URL in the current context
  58. string queryString = "?userid=" + requestContext.RouteData.Values["userid"];
  59. HttpContext.Current.RewritePath(
  60. string.Concat(
  61. VirtualPath,
  62. queryString));
  63.  
  64. var page = BuildManager.CreateInstanceFromVirtualPath
  65. (VirtualPath, typeof(Page)) as IHttpHandler;
  66. return page;
  67. }
  68. }
  69.  
  70. protected void Page_Load(object sender, EventArgs e)
  71. {
  72. string id = Page.Request.QueryString["userid"];
  73. switch (id)
  74. {
  75. case "1234":
  76. lblUserId.Text = id;
  77. lblUserName.Text = "Bill";
  78. break;
  79. case "1235":
  80. lblUserId.Text = id;
  81. lblUserName.Text = "Claire";
  82. break;
  83. case "1236":
  84. lblUserId.Text = id;
  85. lblUserName.Text = "David";
  86. break;
  87. default:
  88. lblUserId.Text = "0000";
  89. lblUserName.Text = "Unknown";
  90. break;
  91. }
  92.  
  93. <system.web>
  94. <compilation debug="true">
  95. <assemblies>
  96. …
  97. <add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  98. </assemblies>
  99. </compilation>
  100. …
  101. <httpModules>
  102. …
  103. <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  104. </httpModules>
  105. </system.web>
  106. <system.webServer>
  107. …
  108. <modules>
  109. …
  110. <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  111. </modules>
  112. <handlers
  113. …
  114. <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
  115. </handlers>
  116. </system.webServer>
  117.  
  118. void Application_Start(object sender, EventArgs e)
  119. {
  120. RegisterRoutes(RouteTable.Routes);
  121. }
  122.  
  123. public static void RegisterRoutes(RouteCollection routes)
  124. {
  125. routes.Add("UseridRoute", new Route
  126. (
  127. "userid/{userid}",
  128. new CustomRouteHandler("~/users.aspx")
  129. ));
  130. }
  131.  
  132. using System.Web.Compilation;
  133. using System.Web.UI;
  134. using System.Web;
  135. using System.Web.Routing;
  136.  
  137. public interface IRoutablePage
  138. {
  139. RequestContext RequestContext { set; }
  140. }
  141.  
  142. public class CustomRouteHandler : IRouteHandler
  143. {
  144. public CustomRouteHandler(string virtualPath)
  145. {
  146. this.VirtualPath = virtualPath;
  147. }
  148.  
  149. public string VirtualPath { get; private set; }
  150.  
  151. public IHttpHandler GetHttpHandler(RequestContext
  152. requestContext)
  153. {
  154. var page = BuildManager.CreateInstanceFromVirtualPath
  155. (VirtualPath, typeof(Page)) as IHttpHandler;
  156.  
  157. if (page != null)
  158. {
  159. var routablePage = page as IRoutablePage;
  160.  
  161. if (routablePage != null) routablePage.RequestContext = requestContext;
  162. }
  163.  
  164. return page;
  165. }
  166. }
  167.  
  168. using System;
  169. using System.Collections.Generic;
  170. using System.Linq;
  171. using System.Web;
  172. using System.Web.UI;
  173. using System.Web.UI.WebControls;
  174. using System.Web.Routing;
  175.  
  176. public partial class users : System.Web.UI.Page, IRoutablePage
  177. {
  178. protected RequestContext requestContext;
  179.  
  180. protected object RouteValue(string key)
  181. {
  182. return requestContext.RouteData.Values[key];
  183. }
  184.  
  185. protected void Page_Load(object sender, EventArgs e)
  186. {
  187. string id = RouteValue("userid").ToString();
  188. switch (id)
  189. {
  190. case "1234":
  191. lblUserId.Text = id;
  192. lblUserName.Text = "Bill";
  193. break;
  194. case "1235":
  195. lblUserId.Text = id;
  196. lblUserName.Text = "Claire";
  197. break;
  198. case "1236":
  199. lblUserId.Text = id;
  200. lblUserName.Text = "David";
  201. break;
  202. default:
  203. lblUserId.Text = "0000";
  204. lblUserName.Text = "Unknown";
  205. break;
  206. }
  207. }
  208.  
  209. #region IRoutablePage Members
  210.  
  211. public RequestContext RequestContext
  212. {
  213. set { requestContext = value; }
  214. }
  215.  
  216. #endregion
  217. }
  218.  
  219. using System;
  220. using System.Collections.Generic;
  221. using System.Linq;
  222. using System.Web;
  223. using System.Web.Mvc;
  224. using System.Web.Mvc.Ajax;
  225.  
  226. namespace MvcApplication1.Controllers
  227. {
  228. public class UsersController : Controller
  229. {
  230. public ActionResult Index()
  231. {
  232. return View(Models.UserDB.GetUsers());
  233. }
  234. public ActionResult userid(int id)
  235. {
  236. return View(Models.UserDB.GetUser(id));
  237. }
  238. }
  239. }
  240.  
  241. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="MvcApplication1.Views.Index" %>
  242. <%@ Import Namespace="MvcApplication1.Controllers" %>
  243. <%@ Import Namespace="MvcApplication1.Models" %>
  244.  
  245. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  246.  
  247. <html xmlns="http://www.w3.org/1999/xhtml" >
  248. <head runat="server">
  249. <title></title>
  250. </head>
  251. <body>
  252. <div>
  253. <h2>Index of Users</h2>
  254. <ul>
  255. <% foreach (User user in (IEnumerable)ViewData.Model) { %>
  256. <li>
  257. <%= Html.ActionLink(user.name, "userid", new {id = user.id })%>
  258. </li>
  259. <% } %>
  260. </ul>
  261. </div>
  262. </body>
  263. </html>
  264.  
  265. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="userid.aspx.cs" Inherits="MvcApplication1.Views.Users.userid" %>
  266. <%@ Import Namespace="MvcApplication1.Controllers" %>
  267. <%@ Import Namespace="MvcApplication1.Models" %>
  268.  
  269. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  270. <html xmlns="http://www.w3.org/1999/xhtml">
  271. <head runat="server">
  272. <title></title>
  273. </head>
  274. <body>
  275. <div>
  276. <table border ="1">
  277. <tr>
  278. <td>
  279. ID
  280. </td>
  281. <td>
  282. <%=((User)ViewData.Model).id %>
  283. </td>
  284. </tr>
  285. <tr>
  286. <td>
  287. Name
  288. </td>
  289. <td>
  290. <%=((User)ViewData.Model).name %>
  291. </td>
  292. </tr>
  293. </table>
  294. </div>
  295. </body>
  296. </html>
  297.  
  298. using System;
  299. using System.Collections.Generic;
  300. using System.Linq;
  301. using System.Web;
  302.  
  303. namespace MvcApplication1.Models
  304. {
  305. public class UserDB
  306. {
  307. private static List<User> users = new List<User>{
  308. new User(){id=12345, name="Bill"},
  309. new User(){id=12346, name="Claire"},
  310. new User(){id=12347, name="David"}
  311. };
  312.  
  313. public static List<User> GetUsers()
  314. {
  315. return users;
  316. }
  317.  
  318. public static User GetUser(int id)
  319. {
  320. return users.First(user => user.id == id);
  321. }
  322.  
  323. }
  324.  
  325. public class User
  326. {
  327. public int id { get; set; }
  328. public string name { get; set; }
  329. }
  330. }
Add Comment
Please, Sign In to add comment