[英]Routing in ASP.NET
我需要在ASP.NET应用程序中使用带有参数的路由。
public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
private void RegisterRoutes()
{
var routes = RouteTable.Routes;
routes.MapPageRoute(
"Profile",
String.Format("{0}/{{{1}}}/", "Profile", "Id"),
"~/Views/Account/Profile.aspx",
false,
new RouteValueDictionary {{"Id", null}});
}
}
然后,通过导航到“/个人资料 ”我想在Page_Load方法Request.Params [“ID”]为空 ,并通过导航到“/资料/ 1”,Request.Params [“ID”]为“1”。
我在哪里弄错了?
使用传统的WebForms,我在RegisterRoutes()方法中创建了两个路由。
routes.Add("profile", new Route("profile",
new CustomRouteHandler("~/profile.aspx")));
routes.Add("profileId", new Route("profile/{id}",
new CustomRouteHandler("~/profile.aspx")));
CustomRouteHandler看起来像这样:
public class CustomRouteHandler : IRouteHandler
{
public CustomRouteHandler(string virtualPath)
{
this.VirtualPath = virtualPath;
}
public string VirtualPath { get; private set; }
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string queryString = "";
HttpRequest request = HttpContext.Current.Request;
string id = Convert.ToString(requestContext.RouteData.Values["id"]);
if (id.Length > 0)
{
queryString = "?id=" + id;
}
HttpContext.Current.RewritePath(
string.Concat(
VirtualPath,
queryString));
var page = BuildManager.CreateInstanceFromVirtualPath
(VirtualPath, typeof(Page)) as IHttpHandler;
return page;
}
}
使用ASP.NET url路由可以实现这一点。 这是一个示例http://newapputil.blogspot.in/2013/12/aspnet-40-web-forms-url-routing.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.