This is more of an ASP.NET Routing question than an ASP.NET MVC question, but any guidance on how to handle a routing parameter with a / in it? For example, say that I am creating a website to show Northwind information and I have a URL pattern like:
/Categories/CategoryName
Which shows the products in the category CategoryName. I have a page that lists all categories with each category name a link to the above URL pattern. I use the RouteCollection object's GetVirtualPath method, specifying the route name to use and
the category name value. Problem is, some of the category names have forward slashes in them (/), like "Meat/Produce". The net effect is that the URL generated for the Meat/Produce link is:
/Categories/Meat/Produce
My workaround has been to create a helper method that I call that replaces all forward slashes with dashes before calling GetVirtualPath, the net result being that the URL gets changed to:
/Categories/Meat-Produce
Then in my RouteHandler class I do the reverse, replacing - with /, before doing the database query to get information about the category in question.
This feels hokey. Is there a better approach, something built into the ASP.NET Routing system to handle such scenarios?
TIA
routing
Happy Programming!
-- Scott Mitchell
-- mitchell@4guysfromrolla.com
-- http://scottonwriting.net/sowblog/
-- http://www.4GuysFromRolla.com/ScottMitchell.shtml
Object reference not set to an instance of an object.
Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 22: var display = System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(_virtualPath, typeof(Page)) as ISMODisplay;
Line 23:
Line 24: display.uname = requestContext.RouteData.Values["uname"] as string;
Line 25: return display;
Line 26: }
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Routing;
using
System.Web.Compilation;
using System.Web.UI;
///
<summary>
///
Summary description for SMONameHandler
///
</summary>
public
class
SMONameHandler :
IRouteHandler
{
public SMONameHandler(string virtualPath)
{
_virtualPath = virtualPath;
}
public
IHttpHandler GetHttpHandler(RequestContext requestContext)
{
var display = System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(_virtualPath,
typeof(Page))
as
ISMODisplay;
You have to implement interface in your web form class - there is a hint in the original article, below the interface definition
("you've defined an interface for your Web Form to implement"), for more details please refer to article
source code:
public partial class RecipeDisplay : Page, ISMODisplay // IRecipeDisplay in the original code
{
//...
}
nothing crashes and burns but i don't get the data i would expect on the page - unless i pass it localhost/smo/members/list2.aspx?uname=jeff, just ran a sql trace and the proc is not being called...
System.Web.Routing.RouteTable.Routes.Add("listing",
new System.Web.Routing.Route("dir/{uname}",
new Smo.Route.Rewrite.SMONameHandler("~/members/list2.aspx")));
Scott Mitchell
Contributor
4114 Points
712 Posts
ASPInsiders
MVP
ASP.NET Routing Question: Routing Parameter with a /
Apr 30, 2009 03:05 PM|LINK
This is more of an ASP.NET Routing question than an ASP.NET MVC question, but any guidance on how to handle a routing parameter with a / in it? For example, say that I am creating a website to show Northwind information and I have a URL pattern like:
/Categories/CategoryName
Which shows the products in the category CategoryName. I have a page that lists all categories with each category name a link to the above URL pattern. I use the RouteCollection object's GetVirtualPath method, specifying the route name to use and the category name value. Problem is, some of the category names have forward slashes in them (/), like "Meat/Produce". The net effect is that the URL generated for the Meat/Produce link is:
/Categories/Meat/Produce
My workaround has been to create a helper method that I call that replaces all forward slashes with dashes before calling GetVirtualPath, the net result being that the URL gets changed to:
/Categories/Meat-Produce
Then in my RouteHandler class I do the reverse, replacing - with /, before doing the database query to get information about the category in question.
This feels hokey. Is there a better approach, something built into the ASP.NET Routing system to handle such scenarios?
TIA
routing
-- Scott Mitchell
-- mitchell@4guysfromrolla.com
-- http://scottonwriting.net/sowblog/
-- http://www.4GuysFromRolla.com/ScottMitchell.shtml
CW2
Participant
938 Points
207 Posts
Re: ASP.NET Routing Question: Routing Parameter with a /
Apr 30, 2009 03:34 PM|LINK
The following route
routes.MapRoute("Categories", "Categories/{*category}", new { controller = "Categories", action = "Details" });
captures whatever is present after '/' character (if anything) and passes it to controller action, where you can process it (split, etc.)
public ActionResult Details(string category)
{
string[] parts = category.Split("/");
//...
}
To generate the url, just pass category name that contains embedded '/' characters, like
Html.ActionLink<CategoriesController>(c => c.Details("Meat/Produce"), "Text");
Scott Mitchell
Contributor
4114 Points
712 Posts
ASPInsiders
MVP
Re: ASP.NET Routing Question: Routing Parameter with a /
Apr 30, 2009 06:06 PM|LINK
Thank you, CW2. That did the trick. [:)]
-- Scott Mitchell
-- mitchell@4guysfromrolla.com
-- http://scottonwriting.net/sowblog/
-- http://www.4GuysFromRolla.com/ScottMitchell.shtml
info@learnba...
Member
307 Points
121 Posts
Re: ASP.NET Routing Question: Routing Parameter with a /
May 02, 2009 04:07 PM|LINK
Server Error in '/MassoHost' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
using
System;using
System.Collections.Generic;using
System.Linq;using
System.Web;using
System.Web.Routing;using
System.Web.Compilation; using System.Web.UI;
/// <summary>///
Summary description for SMONameHandler///
</summary>public
class SMONameHandler : IRouteHandler{
public SMONameHandler(string virtualPath){
_virtualPath = virtualPath;
}
public IHttpHandler GetHttpHandler(RequestContext requestContext){
var display = System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(_virtualPath, typeof(Page)) as ISMODisplay;display.uname = requestContext.RouteData.Values[
"uname"] as string; return display;}
//public string irtualPath { get; private set; } string _virtualPath;}
interface
ISMODisplay : IHttpHandler{
string uname { get; set; }}
CW2
Participant
938 Points
207 Posts
Re: ASP.NET Routing Question: Routing Parameter with a /
May 04, 2009 05:30 PM|LINK
In all likelihood, the value of 'display' variable is null. What are you trying to achieve?
Note: It is better to start a new thread, instead of re-opening already answered one with irrelevant question.
info@learnba...
Member
307 Points
121 Posts
Re: ASP.NET Routing Question: Routing Parameter with a /
May 04, 2009 07:05 PM|LINK
i was trying to follow this msdn article
http://msdn.microsoft.com/en-us/magazine/2009.01.extremeaspnet.aspx
not sure why it's null - but yeah that is what's going on - the global.asax file should be calling
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
no?
CW2
Participant
938 Points
207 Posts
Re: ASP.NET Routing Question: Routing Parameter with a /
May 04, 2009 07:30 PM|LINK
You have to implement interface in your web form class - there is a hint in the original article, below the interface definition ("you've defined an interface for your Web Form to implement"), for more details please refer to article source code:
public partial class RecipeDisplay : Page, ISMODisplay // IRecipeDisplay in the original code
{
//...
}
info@learnba...
Member
307 Points
121 Posts
Re: ASP.NET Routing Question: Routing Parameter with a /
May 05, 2009 05:14 AM|LINK
CW2,
localhost/smo/dir/jeff
nothing crashes and burns but i don't get the data i would expect on the page - unless i pass it localhost/smo/members/list2.aspx?uname=jeff, just ran a sql trace and the proc is not being called...
CW2
Participant
938 Points
207 Posts
Re: ASP.NET Routing Question: Routing Parameter with a /
May 05, 2009 06:03 AM|LINK
What does the route definition look like? Do you have "{uname}" part there?
info@learnba...
Member
307 Points
121 Posts
Re: ASP.NET Routing Question: Routing Parameter with a /
May 05, 2009 06:08 AM|LINK
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startupRegisterRoutes();
}private static void RegisterRoutes()
{
System.Web.Routing.RouteTable.Routes.Add("listing", new System.Web.Routing.Route("dir/{uname}", new Smo.Route.Rewrite.SMONameHandler("~/members/list2.aspx")));}