ASP.NET MVC Web Application can't find href link - html

I have the following HTML in my Azure Web Application:
Please complete and return the
Terms of Service
The .csproj file does have this pdf included in it:
When I click "Launch Google Chrome" to test locally in Visual Studio and when I click on "Terms of Service", I get the following error:
But the file is there! It is on my file system. There is no encoding necessary. Why is this error shown and why can't the PDF be downloaded from the web app? Do I need another step to publish it? Please help!

By default, contents of Views folders are not supposed to be accessed by the normal request pipeline. The Views folder is home for the view files (.cshtml) which will be executed by the view engine. If you look at the web.config file located inside ~/Views you can see that we have a BlockViewHandler registered which prevents direct file access to all files.
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode"
type="System.Web.HttpNotFoundHandler" />
</handlers>
You should consider moving your pdf file to a Statics directory in the app root and point to that.
Now, point to that location
Terms of Service
You can give your own name for the directory. Some people like to keep it under the Content directory. So it is more of a personal preference.
In asp.net core, the wwwroot directory is the static content home by default. So you may keep your static contents inside that (this is overridable)
You could make changes to the web.config located in the views directory to allow direct browsing, but the more solid approach is to not pollute the convention and move the static assets to it's own folder. If you absolutely want to enable direct file access to the view directory, you can update the path attribute value to tell mvc to prevent access to only files with a specific extension. Here is how you will prevent only .cshtml files ( so pdf will work)
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*.cshtml" verb="*" preCondition="integratedMode"
type="System.Web.HttpNotFoundHandler" />
</handlers>

Related

Multiple Web Config Files

So, I have an application that needs multiple Webconfigs. I seperated each Web form in its own folder and put a web config file.
How do I make sure that Login Webform only uses the LoginWeb.Config?
This is my root Web Config and that is what it's using.
I've done searches, and the closest thing I came too was this in my LoginWeb.config file
<appSettings>
<add key="theme" value="Login theme"/>
</appSettings>
No idea how to link that to my Login.aspx web form so it can use that specific config file. Help would be appreciated.

Unable to open Json file on Server

I published a json file on to my website using ftp. When I tried to access my site using mtsite.com/abc.json it throws the below error.
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
My file is on hosted site on azure. Even if file in on server why did it say that file is not found?
Any place in azure portal where I can add or allow Mime type json? I cannot add web.config file as My files are all HTML and Json.
By default IIS (and so the Azure App Service) doesn't serve .json files. You need to enable this feature in your web.config. If don't already have one, create it in the root directory.
Open your web.config and place this code
<staticContent>
<remove fileExtension=".json"/>
<mimeMap fileExtension=".json" mimeType="application/json"/>
</staticContent>
under the nodes
<configuration>
<system.webServer>
And it should be fixed.
Here you can find a basic web.config template that i've made a while ago
P.S: Even though you are using only HTML and JSON, this is the only way to go (as there is not a UI solution in the portal). In this way you could also control other aspect of your website, for example the redirect to HTTPS for the HTTP requests.

I cannot download certain file types from website. In particular .frm, .frx, .mgf. Getting HTTP 404 error

I cannot download certain file types from website. In particular .frm, .frx, .mgf. Getting error: HTTP Error 404 - File or directory not found. I added these file types in IIS as local, it did not help. Can I add any kind of code to the webpage to allow downloading of such files?
All need to be done on the website hosting server, not on downloading computer. Daaahhh. Make sure your hosting account was switched to IIS7.
web.config file need to be created (can be done in Notepad) and placed in the root directory of the website.
web.config file for allowing download of .mgf files:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".mgf" mimeType="text/plain" />
</staticContent>
</system.webServer>
</configuration>

First time uploading website,how to make default web page?

i created a website using asp.net visual studio 2010 express
today i purchased a hosting program with plesk 11.
on the welcome email of the hosting services i got this line: "please be sure that your homepage is saved as an "index" file e.g., index.htm, index.html, etc
all my website is .aspx files!
also my homepage is using a master page!
they help support couldn't help me.
so i ask you can i just rename it to index.aspx and upload it as it is?
Open your web.config file and set the following property in it.
<system.webServer>
<defaultDocument>
<files>
<clear />
<add value="Path of your Page" />
</files>
</defaultDocument>
</system.webServer>

loading .json files generates 404 errors

My first Azure website is a simple test site I've had for a while that makes ajax calls back to the server for JSON data. All the data files have .json extensions. Azure will not 'see' these files. If I change the extension to .txt it servers them up fine.
Do I have to muck with IIS to get this .json to be seen?
I too found Ahmed Sabbour's blog post helpful, but it created an additional problem for me. Whilst the fix worked for the Azure Web App when I then tried to run the app locally it died horribly, throwing HTTP 500.19 everywhere. It took me a while to figure out how to fix this whilst maintaining a single web.config. The solution was (albeit a bit of a fudge) to remove the fileExtension first and then add it back in:
<staticContent>
<remove fileExtension=".json" />
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
For my narrow purposes this was fine and I hope this might save someone time trying to figure this out.
Citing Ahmed Sabbour with his blog post http://blogs.msdn.com/b/africaapps/archive/2013/06/07/how-to-serve-static-json-files-from-a-windows-azure-website.aspx you have to do the following:
"If you upload a .json file to your Windows Azure website and try to access it, it would give you a 404 File Not Found error, because the MIME Type of .json is not set by default. This also applies in general to any file that might need a specific MIME Type.
To fix this issue, FTP into your website and upload the following Web.config file which will set the correct MIME types. If you already have a Web.config file in place, just add the below to the appropriate section.
<?xml version="1.0"?>
<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
</system.webServer>
</configuration>
". I did this and the 404 was gone.
It appears that Azure (Cloud Services, at least) knows how to serve JSON from a deployed ASP.NET MVC project. The problem in my case was that the Build Action on the JSON file's property page was not set correctly. Changing the Build Action to Content fixed it for me.
have you tried adding the specific MIME type to your server's config file?
http://www.iis.net/configreference/system.webserver/staticcontent/mimemap
If you add the mime type as
application/json; charset=utf
that should work.