4

I have an app with links like so in default.aspx...

<link href="Styles/smoothness/jquery-ui-1.8.14.custom.css" rel="stylesheet" type="text/css" />
<script src="Scripts/json2.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-ui-1.8.14.custom.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.tmpl.min.js" type="text/javascript"></script>  

Once I added URL Routing it broke my relative paths. So if I get deep into a route then the page can't find images, scripts, etc. I would imagine that my jQuery service calls are probably broken as well. Is there a way other than adding "/" to the beginning of every relative reference to fix this. In my global.asax I currently have...

void RegisterRoutes(RouteCollection routes)
{

    routes.MapPageRoute(
        "EntityRoute", 
        "{entityID}", 
        "~/Default.aspx");        

    routes.MapPageRoute(
        "GlobalSearchRoute",
        "{entityID}/{guarantorID}/{guarDOB}",
        "~/Default.aspx");
}

Can I add something here that would allow my relative paths to function without changing all of those paths in the site?

2 Answers 2

1

You can use Routes.IgnoreRoute in global.asax to exclude those routes that reference your static content:

routes.IgnoreRoute("Styles/{*pathInfo}");
routes.IgnoreRoute("Scripts/{*pathInfo}");
routes.IgnoreRoute("Images/{*pathInfo}");

etc.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, apparently, but the notation is slightly different: stackoverflow.com/questions/273447/…
It occurred to me that this is a client side problem by the time this happens so the only way to fix this would be in the code itself. I would either have to move away from the relative reference of "Images/" to "/Images/" or I would have to wrap serverside code around those links and preprocess them before they exploded in the browser - like with <%= Url.Content( or something like that. I ended up just fixing the problem by using the absolute reference "/". Thanks for trying to help me though.
routes.IgnoreRoute is a method of Class RouteCollectionExtensions inside System.Web.Mvc Namespace use routes.Ignore for WebForms
I wish we could find a proper solutoin to this. My issue is that my websites are setup in to virtual directories. So I'll have to add "/vir-dir-name", and I really don't want to do that.. :-/
1

You can simply use the html base tag.

In the aspx:

<base id="baseControl" runat="server" />

In the code behind:

protected void Page_Init(object sender, EventArgs e)
{
    baseControl.Attributes.Add("href", Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/"));
}

Hope this helps.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.