3

I have a standard MVC3 project with layout page etc. Now I need to make pretty URLs. I started playing with URL rewrite module. I'm trying to translate http://localhost/Photographer/Pablointo http://localhost/category-about.aspx?displayName=Pablo, and here is my rewrite rule (very simple!):

  <system.webServer>
    <rewrite>
      <rules>
        <rule name="about" patternSyntax="Wildcard">
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_URI}" pattern="\.png|\.js|\.css|\.jpg" negate="true" />
          </conditions>
          <match url="photographer/*" />
          <action type="Rewrite" url="category-about.aspx?displayName={R:1}" logRewrittenUrl="true" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

all conditions you see I added after googling trying to solve the issue - they did not help though.

I found this page: http://www.iis.net/learn/extensions/url-rewrite-module/url-rewriting-for-aspnet-web-forms - which says that ~ operator is properly treated by the server when rewriting rules are applied. But that's clearly does not happen in my case - please see the image attached:

image attached

What is the solution to my problem? How should I reference CSS/JS files? I'm using MVC3 on IIS 7.5.

UPDATE: image is not very clear - but it shows that my MasterLayout page has

<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

but it's resolved as

http://localhost/Photographer/Content/Site.css - and it gives 404

instead of

http://localhost/Content/Site.css - which gives 200

when I request this URL: http://localhost/Photographer/Pablo. Logic works fine - my controller gets the request and renders the page - but it's CSS and images are missing (because they have wrong root folder prepended).

avs099
  • 10,937
  • 6
  • 60
  • 110

3 Answers3

4

Try using Request.ApplicationPath. Something like this should work:

<link href="@(Request.ApplicationPath + "Content/Site.css")" rel="stylesheet" type="text/css" />
  • There seems to be a bug in Url.Content when rewrite rules are in place. I've run into a few other posts describing this exact issue but no detailed information about the root cause. I've actually updated my response since there would be no need to actually use Url.Content if you're relying on Request.ApplicationPath to add the app root to your paths. – Todd McGregor Sep 05 '13 at 15:18
  • Note that Request.ApplicationPath does not include a trailing slash. I tried to edit the answer to update it but it said my update had to be 6 characters. I didn't feel like finding 5 other characters to change. – BVernon Dec 12 '17 at 22:57
2

You said the line:

<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" />

is resolved as

"http:// localhost/Photographer/Content/Site.css"

, which is absolutely correct this is how it would be resolved. Where does your css lie, is the path for the image in css correct?

Imad Alazani
  • 6,688
  • 7
  • 36
  • 58
Shashank Chaturvedi
  • 2,756
  • 19
  • 29
  • why do you think it's correct? it should be http://localhost/Content/Site.css – avs099 Sep 04 '13 at 13:02
  • Because "~" takes you to the root virtual directory, now the question is how you have it mapped in the IIS. On starting the application what is the URL you get? – Shashank Chaturvedi Sep 04 '13 at 13:12
  • See this link:http://stackoverflow.com/questions/10482299/iis-7-rewrite-rule-and-url-content-issues Hope it helps. By the time I will try and replicate issue you are facing. – Shashank Chaturvedi Sep 04 '13 at 15:22
0

Rather than this

<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

Try this without the tilde (~)

<link href="@Url.Content("/Content/Site.css")" rel="stylesheet" type="text/css" />

This should resolve to your desired http://localhost/Content/Site.css

Luke Baughan
  • 4,658
  • 3
  • 31
  • 54