开发者

Regarding URL Re-write using routing concept

开发者 https://www.devze.com 2023-04-01 03:17 出处:网络
if possible then please provide me code for implementing url routing. suppose i have two url in my page like

if possible then please provide me code for implementing url routing. suppose i have two url in my page like

first one


www.mysite.com/101/category.aspx

so i want that when user click on the above url then request should goes like

www.mysite.com/category.aspx?id=101 but

www.mysite.com/101/category.aspx this url should show in the addressba.

when any user will directly type url like www.mysite.com/category.aspx?id=101 then no routing will happen rather above url should process and

www.mysite.com/category.a开发者_JAVA技巧spx?id=101 this url should show in the addressbar.

second one


www.mysite.com/audi/product.aspx

so i want that when user click on the above url then request should goes like

www.mysite.com/product.aspx?cat=audi but

www.mysite.com/audi/product.aspx this url should show in the addressbar.

when any user will directly type url like www.mysite.com/product.aspx?cat=audi then no routing will happen rather above url should process and

www.mysite.com/product.aspx?cat=audi this url should show in the addressbar.

i have never work with url routing....so please guide me in terms of coding. thanks


In ASP.NET using the IIS7 rewrite module you would use something like this:

<rewrite>
  <rules>
    <rule name="Rewrite to category.aspx">
      <match url="^([0-9]+)/category.aspx" />
      <action type="Rewrite" url="category.aspx?id={R:1}" />
    </rule>
    <rule name="Rewrite to product.aspx">
      <match url="^(.*)/product.aspx" />
      <action type="Rewrite" url="product.aspx?cat={R:1}" />
    </rule>
  </rules>
</rewrite>

Updated as realised you can't pass routing variables as querystring with MapPageRoute as I originally showed. In fact things get tricky if you want to do this. There's 2 options I can think of.

Option 1)

Use the following route.

routes.MapPageRoute(
   "category",
   "{category}/category.aspx",
   "category.aspx"
);

Then use the following code in your category.aspx instead of querystring to extract the category value.

ControllerContext.RouteData.Values["category"];

Option 2)

This involves creating custom handler to rewrite the RouteData into the querystring when the path is rewritten.

public class PageRouteWithQueryStringHandler : PageRouteHandler
{
    public RouteWithQueryHandler(string virtualPath, bool checkPhysicalUrlAccess)
        : base(virtualPath, checkPhysicalUrlAccess)
    {
    }

    public RouteWithQueryHandler(string virtualPath)
        :base(virtualPath)
    {
    }

    public override IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        var request = requestContext.HttpContext.Request;
        var query  = HttpUtility.ParseQueryString(request.Url.Query);
        foreach (var keyPair in requestContext.RouteData.Values)
        {
            query[HttpUtility.UrlEncode(keyPair.Key)] = HttpUtility.UrlEncode(
                                               Convert.ToString(keyPair.Value));
        }
        var qs = string.Join("&", query);
        requestContext.HttpContext.RewritePath(
                             requestContext.HttpContext.Request.Path, null, qs);
        return base.GetHttpHandler(requestContext);
    }
}

This can be registered as follows.

routes.Add("category", new Route("{category}/category.aspx",     
           new PageRouteWithQueryStringHandler ("~/category.aspx", true)));
0

精彩评论

暂无评论...
验证码 换一张
取 消