1

Using ASP.NET MVC3 with Razor & C#.

Say I have an application that is set up to run as a normal website through IIS, but now I want to run this application as a sub-application under another website. For example, the sub-application will be stored in a folder called "SubApp" off the root of the website (e.g. www.example.com/SubApp/).

If I reference a URL such as "~/Images/picture.gif" within SubApp's razor mark-up/code-behind, it will resolve to the root of SubApp: www.example.com/SubApp/Images/picture.gif

However, if I reference "/Images/picture.gif" through regular HTML (in SubApp), it will resolve to the root of SubApp's parent website: www.example.com/Images/picture.gif

Is there an easy, reliable way to resolve these HTML URLs to the sub-application's root without rewriting them to use Razor? What's the best way to handle URLs under these circumstances?

Aaron
  • 316
  • 2
  • 8
  • You can use `@Url.Content` inside JavaScript with [some trick](https://stackoverflow.com/a/7663549/1369235). – Himanshu Nov 30 '21 at 12:21

1 Answers1

1

If you're dealing with pure HTML pages that will be requested directly by a browser, you should use relative URLs. For example, if you have:

/SubApp
    /Images
        foo.gif
    page.html

You should use:

<img src="foo.gif" />

Inside page.html. You can use "../" to go up directories, etc.

Unfortunately, if you're dealing with a more complicated server-side routing scenario, you're going to need to use some kind of server-side code to handle that.

Andrew Stanton-Nurse
  • 6,274
  • 1
  • 28
  • 20
  • I'm in the process of converting legacy pages to MVC3, which are a mess of HTML and ASP.NET controls. Unfortunately, the conversion is a side project, so I can't do it all at once. The relative URLs don't play nice with the legacy controls since they're used in multiple places, but I'll keep them in mind for pages that don't use server side processing. Thanks for your input! – Aaron May 01 '12 at 13:46