问题描述:

I am using routing in my .net website.

I want this url

http://www.website.com/condos/rent/{state}/{area}

To go to http://www.website.com/condos.aspx and then pick up the state and area.

This works fine:

routes.MapPageRoute("CondosForRentInArea", "condos/rent/{state}/

{area}", "~/condos.aspx");

But I am having some problems with the Javascript. Because I can't write:

routes.MapPageRoute("StateAreaJS", "condos/rent/{state}/{area}/scripts/

{filename}.js", "~/scripts/{filename}.js");

I found another question here on Stackoverflow: Wildcards with ASP.NET MVC MapPageRoute to support organizing legacy code

But I am not using MVC in this website. I have tried this:

routes.Add("CondosRentStateAreaJS", new Route("condos/rent/{state}/{area}/scripts/

{filename}.js", new StateAreaJSRouteHandler()));

And:

public class StateAreaJSRouteHandler : IRouteHandler

{

public IHttpHandler GetHttpHandler(RequestContext requestContext)

{

string filename = requestContext.RouteData.Values["filename"] as string;

if (string.IsNullOrEmpty(filename))

{

requestContext.HttpContext.Response.Clear();

requestContext.HttpContext.Response.StatusCode = 404;

requestContext.HttpContext.Response.End();

}

else

{

requestContext.HttpContext.Response.Clear();

requestContext.HttpContext.Response.ContentType = "text/javascript";

// find physical path to image here.

string filepath = requestContext.HttpContext.Server.MapPath("~/scripts/" + filename + ".js");

requestContext.HttpContext.Response.WriteFile(filepath);

requestContext.HttpContext.Response.End();

}

return null;

}

}

Nut I don't think the code is called, because placing a break point in the line "string filename..." never hits.

Strangely enough, the CSS is there and the server inserts ../../../ before the folder/filename but the JS is missing.

So how do I correctly get /state/area/scripts/file.js to be just "~/scripts/file.js"?

--------------- EDIT ---------------

You definitely got me on the right track. I should add the scripts and stylesheets through the code-behind. I ended up getting a great solution like this:

HtmlGenericControl validatejs = new HtmlGenericControl("script");

validatejs.Attributes.Add("type", "text/javascript");

validatejs.Attributes.Add("src", ResolveUrl("~/scripts/validate.js"));

this.Page.Header.Controls.Add(validatejs);

HtmlLink fontscss = new HtmlLink();

fontscss.Href = "~/styles/fonts.css";

fontscss.Attributes.Add("rel", "stylesheet");

fontscss.Attributes.Add("type", "text/css");

this.Page.Header.Controls.Add(fontscss);

AND I just want to add (for other googlers) that this approach has enabled me to remove my JS and CSS CustomRouteHandlers, making the whole project more organized and probably faster.

网友答案:

Can you just put the script reference in your master page? That way it will available and make it a lot easier.

网友答案:

You can also put the reference on the page it's being used on, or use the code below on your masterpage as suggested by Ashok. On the front side, use a literal and on the code behind do this (obviously change to your names):

litJquery.Text = "<script type=\"text/javascript\" src=\"" + Page.ResolveClientUrl("~/js/jquery-1.6.min.js") + "\"></script>";

Good Luck!

相关阅读:
Top