Webruster Webruster - 1 month ago 11
ASP.NET (C#) Question

Routing based on same parameter(s) pattern and different pages in web forms

I am implementing Routing in web forms :

These is my custom routings

public static void MyCustomRoutes(RouteCollection routes)
{
routes.Ignore("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("NewsByTitle",
"{NewsTitle}",
"~/News.aspx");

routes.MapPageRoute("BlogsByTitle",
"{BlogsTitle}",
"~/ViewBlogs.aspx");
}


In my default page i'm having Blogs and News sections , when i'm clicking on
News
it is navigating to the News page as it is defined first in the routing table.

but when i'm clicking on
Blogs
it is taking the route of News only .

Here is my
RedirectToRoute
for
Blogs
and
News


News:

String Url = clsMethods.GetTileByStoryId(BlogId); //My Url Param
Response.RedirectToRoute("NewsByTitle", new { NewsTitle = Url });


Blogs

String Url = clsMethods.GetTileByStoryId(BlogId);
Response.RedirectToRoute("BlogsByTitle", new { BlogsTitle = Url});


Update

As per Mihir Suggestion i have created the Custom Constraint so that will be solving my need here this is how i implemented the Constraint Logic

public static void MyCustomRoutes(RouteCollection routes)
{
routes.Ignore("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("NewsByTitle",
"{NewsTitle}",
"~/News.aspx",
false,
null,
new RouteValueDictionary
{ { "checkNewsRoute", new IsNewsConstraint() } });

routes.MapPageRoute("BlogsByTitle",
"{BlogsTitle}",
"~/ViewBlogs.aspx",
false,
null,
new RouteValueDictionary
{ { "checkRoute", new IsBlogConstraint()} });

}


Here is that Constraint

Blogs Constraint

public class IsBlogConstraint : IRouteConstraint
{
public bool Match
(
HttpContextBase httpContext,
Route route,
string parameterName,
RouteValueDictionary values,
RouteDirection routeDirection
)
{
return clsMethods.checkRoute(Convert.ToString(values["BlogsTitle"]));

}
}


News Constraint

public class IsNewsConstraint : IRouteConstraint
{
public bool Match
(
HttpContextBase httpContext,
Route route,
string parameterName,
RouteValueDictionary values,
RouteDirection routeDirection
)
{
return clsMethods.checkNewsRoute(Convert.ToString(values["NewsTitle"]));
}
}

Answer

I think you are trying to configure your route to map following URL pattern.

  1. News: http: //www.website.com/title-of-the-news
  2. Blog: http: //www.website.com/title-of-the-blog

As per your configuration if you try to open the blog it will take you to the news page which is a correct behaviour because URL to the blog does match with news route.

To distinguish two different pages you need to configure route with some specific path for both types news and blog like I have done below.

routes.MapPageRoute("NewsByTitle", "news/{NewsTitle}", "~/News.aspx");
routes.MapPageRoute("BlogsByTitle", "blogs/{BlogsTitle}", "~/ViewBlogs.aspx");
  1. News: http: //www.website.com/news/title-of-the-news
  2. Blog: http: //www.website.com/blogs/title-of-the-blog

If you are looking for solution exactly you have mentioned in your question you need to implement route constraints as it is done here. Custom Constraints help routes to prevent a route from being matched unless some custom condition is matched, blog or news in your case.

In that constraint you can write some logic to check that the path segment is a news or a blog and return boolean value. So when the news constraint looks for a blog name it would not find the entry as news (in the db) and returns false and that route would be ignored.