2

I have a MVC 4 web app (http://whatever/myapp/home/someprocess) running under the default web site (on the server) as a separate application and in various places i need to reference files starting at the root mostly in javascript (root = http://whatever/myapp/).

The window.location options in this answer amongst others doesn't work for me as it gets the base url i.e. http://whatever/ instead of http://whatever/myapp/.

Extract from the answer above:

• window.location.host : you'll get sub.domain.com:8080 or sub.domain.com:80
• window.location.hostname : you'll get sub.domain.com
• window.location.protocol : you'll get http:
• window.location.port : you'll get 8080 or 80
• window.location.origin : you'll get http://sub.domain.com

The reason I do not want to manually append the "myapp" part is that i need to change this everytime i run it in express debug mode. Surely I'm missing something simple?

When debugging on my pc it should then implement http://localhost:someport/home/someprocess/ and when publishing to a server it should implement http://server/myapp/home/someprocess.

A line of code I'm currently battling with:

var jqxhr = $.getJSON("/myapp/AutoComplete/LoadComboData", { classname: '@ViewBag.ClassName', functionname: '@ViewBag.MethodName', searchstring: value, context: contextvalue, assembly: '@ViewBag.AssemblyName' })

@*var jqxhr = $.getJSON("/AutoComplete/LoadComboData", { classname: '@ViewBag.ClassName', functionname: '@ViewBag.MethodName', searchstring: value, context: contextvalue, assembly: '@ViewBag.AssemblyName' })*@

As you can see when debugging in VS2013 I comment out the top one, and when the app gets deployed I have to make sure the bottom one is commented out. There HAS to be a better approach?

Community
  • 1
  • 1
KDT
  • 671
  • 1
  • 6
  • 15

1 Answers1

1

Solution 1:

The better approach would be to debug this locally on IIS.

In the project properties => Web tab.

Under Servers select Local IIS from the dropdown, and set the correct Project URL (http://localhost/myapp). Then create the virtual directory.

This way you would not have to switch between those two lines when debugging.

Solution 2:

As a sidenote you could use the IsDebuggingEnabled property and then assign the value to jqxhr accordingly. Modify the debug attribute on the system.web.compilation element to switch between the two different modes.

var jqxhr = []
@if (!HttpContext.Current.IsDebuggingEnabled)
{
   jqxhr = $.getJSON("/myapp/AutoComplete/LoadComboData", { classname: '@ViewBag.ClassName', functionname: '@ViewBag.MethodName', searchstring: value, context: contextvalue, assembly: '@ViewBag.AssemblyName' }) 
}
else
{
    jqxhr = $.getJSON("/AutoComplete/LoadComboData", { classname: '@ViewBag.ClassName', functionname: '@ViewBag.MethodName', searchstring: value, context: contextvalue, assembly: '@ViewBag.AssemblyName' })
}

Solution 3:

Create an extension within the MVC project, which returns a bool whether or not the conditional debug symbol is met.

public static bool IsDebug(this HtmlHelper htmlHelper)
    {
#if DEBUG
      return true;
#else
      return false;
#endif
    }

CSHTML:

var jqxhr = []
@if (!Html.IsDebug())
{
   jqxhr = $.getJSON("/myapp/AutoComplete/LoadComboData", { classname: '@ViewBag.ClassName', functionname: '@ViewBag.MethodName', searchstring: value, context: contextvalue, assembly: '@ViewBag.AssemblyName' }) 
}
else
{
    jqxhr = $.getJSON("/AutoComplete/LoadComboData", { classname: '@ViewBag.ClassName', functionname: '@ViewBag.MethodName', searchstring: value, context: contextvalue, assembly: '@ViewBag.AssemblyName' })
}

Solution 2 and 3 will however need you to change either a attribute within web.config, or change the configuration between Debug/Release. I would however recommend the first solution. I've used that one, and it works like a charm. You even eliminate possible situations where one thing works on the Express webserver, but not on IIS (which saves you for some headache) :-)

Source for solution 2 and 3: Razor view engine, how to enter preprocessor(#if debug)

Community
  • 1
  • 1
scheien
  • 2,457
  • 1
  • 19
  • 22
  • Works like a charm thanks scheien! I thought one could get the virtual directory where the app is running from so the code is dynamic but it doesn't seem possible? If the site moves from myapp to myapp2 I'd have to change the code anyway, at least only once now. ;-) thanks! – KDT Jun 26 '14 at 05:30
  • 1
    Your welcome mate. I've just rewritten with two other possible approaches, but the first one is the best. Glad you got it working, cheers! :-) – scheien Jun 26 '14 at 05:34