How replace '~' symbol in MVC with physical path - asp.net-mvc

In Mvc application if the Layout is added with
#Styles.Render("~/Content/themes/base/css")
#Scripts.Render("~/bundles/jquery") .
Need a solution to replace the
~
symbol .So that Output should not be like
/Content/themes/base/jquery.ui.core.css in render page .
Instead
localhost:xxxxx/Content/themes/base/jquery.ui.core.css .
This Localhost need to be added when mvc application is run under IISExpress wherein It get modified to hosted application path when hosted into the server

As far as I know, the path used in the .Render() calls is just an identifier or key that will produce the paths for the files that were added to it earlier.
This is what you'd normally find in BundleConfig.cs:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
The ~/bundles/ part is never sent to the output when .Render() is called. What is sent are the actual (translated) paths from the .Include() call.
You should always make those paths start with "~/" so they will be output correctly. I have never found the need to include the server name, because the browser will by default request the file from the same server that delivered the web page.

Related

ASP.net MVC bundles https

asp.net mvc bundles do not render for https.
here is my code:
bundles.Add(new StyleBundle("~/Content/css/css").Include("~/Content/css/custom.css"));
View
#Styles.Render("~/Content/css/css")
And the error code I get is:
Mixed Content: The page at 'https://domain/' was loaded over HTTPS, but requested an insecure stylesheet 'http://domain:443/Content/css/custom.css'. This request has been blocked; the content must be served over HTTPS.
Do you have ~/Content/css/css as a real path in your app?
IIS is probably trying to handle the request.
Rename your bundle or the folder in your app and it should work.
So try this:
bundles.Add(new StyleBundle("~/Content/core.css").Include("~/Content/css/custom.css"));
Then in your view:
#Styles.Render("~/Content/core.css")
The style bundle name can be anything you want, it doesn't have to be related to the real location of your CSS file.
Update
If you are getting mixed content errors, that's because some of your content is coming over HTTPS and other content is coming over HTTP.
The best way to overcome this problem is use // on all your content.
So say for example you have an image like this
<img src="http://website.com/images/smiley.gif">
change it too
<img src="//website.com/images/smiley.gif">
That should stop your mixed content warnings.
Use a different bundle folder
Like, Update
bundles.Add(new StyleBundle("~/Content/css/css").Include("~/Content/css/custom.css"));
To
bundles.Add(new StyleBundle("~/CssBundles/css/css").Include("~/Content/css/custom.css"));
This issue happens if you use an existing project folder for the bundle location.

IIS url rewrite ignores .chtml ( Razor ) files

I have a rewrite rule that says.
site.one/content/templates/whatever.cshtml -> site.two/content/templates/whatever.cshtml
The problem occurs when site.one trying to fetch .cshtml files. It returns a 404.
However if i change the extension to .js or .html then the rule works.
If i use the site.two link the request handles it just fine, this is without any rewriterules ofcourse.
site.one can also handle .cshtml files from within its own directory.
Iam using IIS 10, Urlrewrite module 2, and Application Request Routing 3.
UPDATE
So the .cshtml in this case are template files that is fetched with async text.js, and then gets compiled with handlebars as the last step before rendered, but some of them need to get processed by razor engine because of the translations. This works if not rewriting.
Razor files are never served by the MVC framework or IIS. Razor files are processed by MVC and the result of the processing is sent to the output through a controller action method. The server is correctly responding with a 404 response in this case, because these files are blocked by default (you would never want to serve an unprocessed .cshtml file with source code to the user).
Since these files are blocked by IIS, there is no way to redirect them using the URL Rewrite Module (nor should there be).
I am guessing that you probably want to redirect your MVC URLs, which don't have an extension by default. The default way they are accessed (which can be customized) is /{controller}/{action}/. For example, the in the MVC template the default way to get to the about page is through the URL /Home/About/.

Running MVC application on IIS without domain

My application contains lots of links to the root ("/login/dologin"). When I'm running the application under a domain, there's no problem.
Right now I'm moving to a new server, and I can't test my application. My application sits in "localhost/md", I need the link to go to "localhost/md/login/dologin". Instead, it goes to "localhost/login/dologin", and, ofcourse, the resource cannot be found.
What do I need to configure on my IIS to make this works without domain?
Thanks.
It's just a guess, since you haven't posted any of your configuration.
In your authentication element in the web.config, do you have the route to the login page specified as /login/dologin? could you try ~/login/dologin
The second option, should give you a relative path from the home of the virtual directory application, rather than going to the root of the 'site'
For referencing files (e.g. javascript & css) you could do #Url.Content("~/path/to/file.js")
EDIT: Based on additional comments
in Layout.cshtml...
var SITE_ROOT = '#Url.Content("~/")'
then in your JS file use SITE_ROOT as a prefix in your routes
var url = SITE_ROOT + "Home/Index";

ASP.NET MVC server path is different from application path

I have an unusual circumstance where our web server inserts a folder into the url path before loading the page. Let me give you an example:
The app is called equipment and if I were to run it on a normal server setup, it would look like:
www.site.com\equipment\home\index
BUT when I run it on our server, it inserts "idn" in the url:
www.site.com\idn\equipment\home\index
The messes up my relative references. The MVC functions want to redirect to use "\equipment\" instead of "\idn\equipment\". This happens with Scripts.Render(), Return View(), etc.
Also, I can't hardcode the "idn" into my urls b/c then it is no longer relative and it won't work on my dev box or test servers b/c they don't have a "idn" subfolder in localhost.
I also tried functions such as Request.ApplicationPath and what not but none of them return the "idn" in the result.
Is there way to MVC to know that this "idn" was inserted into the url and account for it?
Thanks!
Create your application on the test/production server in the idn folder, then it all works.

ASP.NET MVC 4 Bundling and Minification: wrong paths in Debug Mode with Url Rewrite

I want to use the ASP.NET MVC 4 Bundling and Minification feature, please assume that we run the debug mode so nothing is bundled, but each reference is simply rendered to single tags. Locally everything works fine:
Example
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
}
When calling #Scripts.Render("~/bundles/jquery") from the view now, a tag similar to this is generated:
<script src="/Scripts/jquery-1.8.2.js"></script>
The result is the same if I would just write:
<script src="#Url.Content("~/Scripts/jquery-1.8.2.js")"></script>
But when I deploy everything to a test server in the internet (still using the debug mode), there is some Url rewriting enabled, where I route specific domains or subdomains to specific folders.
For example, if the domain is sub1.example.com then forward to example.com/__sub1__ and in case of sub2.example.com forward to example.com/__sub2__ and so on. When I open sub1.example.com in the browser, there is usually no clue that this forward happens, the Url doesn't change, all Urls remain working and so do references generated using the Url.Content(...) method.
But for some strange reason, the call to #Scripts.Render("~/bundles/jquery") now renders something like this:
<script src="/__sub1__/Scripts/jquery-1.8.2.js"></script>
Please note the "/sub1" part which is the part which shouldn't be generated and never appear somewhere in the rendered html code and leads to a 404 because the url rewrite forward fails.
While...
<script src="#Url.Content("~/Scripts/jquery-1.8.2.js")"></script>
...still renders the correct relative Path to "/Scripts/jquery-1.8.2.js". And i never had other problems with rewriting the Urls like this. And I don't want to get rid of this rewriting.
Obviously, the same applies to style sheets.
Any ideas what I could try?
This is a bug with the current implementation of the script/style helpers, this should be fixed with the 1.1-alpha1 release that should be available shortly(early Oct) as the support for using the VirtualPathProvider registered with ASP.NET has fixed this general class of issue (resolving resource paths correctly).

Resources