locked
Question about an issue with a MVC project and views RRS feed

  • Question

  • User1374623307 posted

    Hello,

       So originally the design aspect of a site that was being developed was supposed to be the mvc app would be the site name, essentially root on IIS.  Development proceeded and everything was working good on the development side.  Well a change was made to make this a subsite (www.sitename.com/mvcapp).  After some discussion that things should work still, it was decided to push the app to the IIS server in UAT and go from there.  An issue did present itself, and we have determined a work around, but I am trying to see if there is a reason behind the issue.

       So the first thing that was notices was that when the app was launched to the first controller (Menu) that the background picture did not display.  We were able to navigate to other sections of the app and most other controllers worked without a problem (www.sitename.com/mvcapp/controller/edit, etc. )  Further inspection of the reason the background were not working and the logo was working showed the background image pulling a 404 error, where it was looking for the image at www.sitename.com/mvcapp/Menu/~/images/BackgroundImages/imagename.png.  Even more baffling was that the logo was stored in the same folder on the wwwroot and it worked.  Then it was noticed that one of the modals was not working correctly as it tried to call up a differentcontroller/index it too was getting a 404 looking at the original controller and appending the next controller onto it so it was seeing Menu/DifferentController/Index.  Now after some playing around with the images the tilde was not working, so it was replaced with a ../images/BackgroundImage/imagename.png and pushed to the server.  This worked.  The site.js was reworked so that the modal was now calling ../DifferentController/Index and it worked.

       I guess the question comes in as to why the dot dot (..) worked and the tilde (~) didn't work?  Everything that was read is that the tilde should have brought it back to the wwwroot folder.  While there is a working solution in place, it there a way to make this work with the tilde or as in the case of the modal not have a tilde or dot dot in front?

    Friday, May 15, 2020 2:57 AM

All replies

  • User-2054057000 posted

    Make sure you used app.UseStaticFiles(); inside the Configure() method of startup class.

    I recommend you to take a look to Configuring Application in ASP.NET Core MVC

    Friday, May 15, 2020 4:43 AM
  • User1374623307 posted

    We do have the app.UseStaticFiles(); in the configuration of the Startup.cs.  That has been there from the beginning.

    Friday, May 15, 2020 10:06 AM
  • User475983607 posted

    I guess the question comes in as to why the dot dot (..) worked and the tilde (~) didn't work?  Everything that was read is that the tilde should have brought it back to the wwwroot folder.  While there is a working solution in place, it there a way to make this work with the tilde or as in the case of the modal not have a tilde or dot dot in front?

    Most likely you added the tilde to code that does not run through .NET like a CSS file.  

    www.sitename.com/mvcapp/Menu/~/images/BackgroundImages/imagename.png.

    Friday, May 15, 2020 10:26 AM
  • User1374623307 posted

    Well it was not used in a .CSS file, it was used in the view.

    Here is an example of how it was used

    <div class="col-lg-6" style="background-image:url('~/images/BackgroundImages/the_grey.jpg'); background-repeat:no-repeat; background-size:auto;">
    

    Whereas the script that had the 404 issue when calling the controller was like:

     open: function (event, ui) {
                $(this).load('/Characters/Index');
            },

    This is why it is baffling as with the startup class having app.UseStaticFiles();  and other files being able to render using the tilde why don't the ones giving the issue work the same way? 

    Friday, May 15, 2020 9:16 PM
  • User475983607 posted

    Well it was not used in a .CSS file, it was used in the view.

    Here is an example of how it was used

    <div class="col-lg-6" style="background-image:url('~/images/BackgroundImages/the_grey.jpg'); background-repeat:no-repeat; background-size:auto;">

    Whereas the script that had the 404 issue when calling the controller was like:

     open: function (event, ui) {
                $(this).load('/Characters/Index');
            },

    This is why it is baffling as with the startup class having app.UseStaticFiles();  and other files being able to render using the tilde why don't the ones giving the issue work the same way? 

    The syntax is a plain HTML.  It's not a tag helper or html helper.   So yes, the tilde is expected.

    Saturday, May 16, 2020 11:02 AM
  • User-474980206 posted

    Well it was not used in a .CSS file, it was used in the view.

    Here is an example of how it was used

    <div class="col-lg-6" style="background-image:url('~/images/BackgroundImages/the_grey.jpg'); background-repeat:no-repeat; background-size:auto;">

    Whereas the script that had the 404 issue when calling the controller was like:

     open: function (event, ui) {
                $(this).load('/Characters/Index');
            },

    This is why it is baffling as with the startup class having app.UseStaticFiles();  and other files being able to render using the tilde why don't the ones giving the issue work the same way? 

    the ~ is only supported in razor views and only in attributes it understands as urls. If you start a url with a / that means start at site root, the dot and double dot are relative paths.

    Saturday, May 16, 2020 4:18 PM