Serving .cshtml files on IIS globally - c#

I am trying to setup intranet IIS 8.5 (Win8.1) to globally serve .cshtml (Razor) files. The corresponding Application Pool is set to v4.0.
The files are simple Web Pages, not MVC. Here is an example of one:
<html>
<body>
#foreach (var i = 0; i < 10; i++)
{
<li>Item #i</li>
}
</body>
</html>
By removing the mapping of .cshtml files to System.Web.ForbiddenHandler on the IIS server's Handler Mappings I was able to get past the initial hurdle of ASP.NET telling me that
This type of file is not served.
However, the .cshtml files are now served verbatim to the browser, instead of being run through the Razor rendering process.
One would think that it should be easy to serve Razor pages from IIS, but it isn't. I need to somehow convince IIS to interpret these pages as Razor views; I suspect I am missing some mapping to the appropriate handler (I don't want MVC though - just simple Web Pages).
Here are some additional constraints:
I would definitely like to avoid including a bin folder with the requisite Razor assemblies in each of the sites on the server. The server hosts many sites, and I don't want to have to copy the bin folder everywhere. It should be possible to configure it globally, once and for all.
Ideally, I would not even need a local web.config for each site. The sites that are being served are a patchwork of technologies, containing .html, .shtml, .php, .asp, .aspx, and - hopefully - .cshtml files and should not be dependent on a single technology or config.
Creating a Visual Studio project is expressly out of the question. I should be able to use any text editor to modify the .cshtml files.
.NET Core is not installed on the machine and is not an option. Must use full Framework up to 4.6.2.
I am aware of many other SO questions that are similar, but don't quite solve my problem.
This question for example, was closed as "unclear" before it could have been answered, yet it was pretty clear to me! I am having the exact same problem.
The accepted answer to this question simply resorts to copying the bin folder. This is something I specifically don't want to do.
This answer says you can run an MVC application without installing MVC on your server, again by copying a bin folder into the local root. I do want to install Razor (but not necessarily MVC) onto my server globally.
Essentially, what I am trying to do is to use Razor syntax in a way reminiscent of classic ASP, or ASPX, without the baggage of MVC.
Can it be done?

I believe what you're looking for is a feature called ASP.NET Web Pages. If you use ASP.NET Core 2.0, the most recent new project templates uses Pages, rather than controllers and views. I've never read this doco, but I guess it should help you get started (or just create an ASP.NET Core 2.0 project from the new project wizard)

Related

How to change file name in Visual studio project?

I have a project solution in visual studio. Its created with MVC .net
I want to move some of my code files such as index.cshtml and others out of the Views>Home folder and into only Views folder. Will this break my project?
Also I want to rename index.cshtml to a new name, throughout all instances in my solution. When I tried this it only did the one file and it broke the project. I tried a CTRL F to find all index.cshtml, but it said there are no instances (though I know there are)
I need to rename the files correctly, so that I can deploy my project to match a current setup.
How do I rename all instances of index.cshtml in visual studio ?
And will moving the files outside of the home folder break the project?
I tried this it only did the one file and it broke the project. I tried a CTRL F to find all index.cshtml, but it said there are no instances (though I know there are)
If you just starting out I'd recommend not moving/renaming view files by hand. Eventually you'll have good understanding of how view discovery works and will be able to move files to places where they can be found at run-time (and even add more places yourself).
ASP.Net is convention-based approach to locate view files - short version is by default runtime looks for .cshtml files with this name format "/views/{controller name}/{action name}.cshtml", you can change view name by specifying different name when returning View("MyOtherView") from controller.
For detailed information on how view discovery works see Order in which Views are searched in MVC, how to change order of search viewLocation in asp.net mvc?, Microsoft - Views in ASP.NET Core MVC.

Deploying an ASP.NET Web API service: precompile during publishing option, better activate it or not?

I have an ASP.NET Web API service. I have noticed that on Web Publishing Wizard there is an option called "Precompile during publishing". What does this option do? What is the impact by publishing with this option checked or unchecked?
I have googled and found this question. What effect does the new precompile during publishing option have on MVC4 applications?.
There it says:
If you have anything in App_Code, it will be precompiled into a DLL
before deployment. Without precompiling, this would happen on the fly
by the ASP.NET runtime.
Based on this I understand that If you have things in App_Code it is better to check this option, also I understand that performance increases, right?
There it also says:
If you don't have any files in App_Code and you want your site to
remain updateable, it doesn't seem to do much.
Where is App_Code folder? In my ASP.NET Web API REST project I do not have any App_Code folder. Instead I have App_Data (empty) and App_Start folders.
So in my case, as I do not have any App_Code folder, is it better to not check "Precompile during publishing" option?

Deploying default.cshtml outside of MVC project

I am using RazorEngine and Template functionality to prepare some pages as default.cshtml. These default.cshtml are very simple with some HTML/JS/CSS code and 1-2 lines of MVC if/else blocks. I need to deploy these files outside of the MVC project and directly under a blank asp.net website running 4.5.2 framework.
Can you please let me know what should be web.config entries and what assembly files are to be deployed in the bin folder. FYI: This website will not have anything except these default.cshtml and web.config/bin/etc., if required.
FYI: I am generating these default.cshmtl files using MVC 5.2.3.0, RazorEngine 3.7.2.0.
You pretty much answered your own question. If you've got a cshtml file, then you need MVC and Razor to render it. If you're using RazorEngine to generate them in the first place, then you could simply output them as *.html instead of *.cshtml, and then you wouldn't need anything. As long as it has Razor code that needs to be processed, you need MVC and Razor.

Deploying multiple ASP.NET MVC application in a single Virtual directory

Normally an ASP.NET MVC application can be deployed by placing the application folder in the virtual Directory.
The application folder contains
bin folder
Scripts Folder(If needed)
Views folder
Global.asax file
web.config file
But, I want to place the bin folder outside the application folder so that I can deploy more applications in a single application folder. All applications share the bin folder and session also get shared.
What I want to do
..Virtual Directory
..\bin
..\App1Name\App1Contents
..\App2Name\App2Contents
..\App3Name\App3Contents
..web.config
I already achieved this for web application with web forms without Global.asax file. But in MVC application I am unable to shared the bin folder.
How to achieve it?
Where I need to place the Global.asax file?
How the route all the applications with bin folder placed outside the Applications?
Thanks in advance
This is not possible. You were able to achieve this with Web Forms because each individual page is in effect it's own application. However, MVC works differently. Many factors, not the least of which is the routing framework, requires that the web application be secluded. You cannot deploy multiple MVC applications to the same document root. You can deploy a second MVC application to a virtual directory within the first, but be advised that the web.config of the outermost application will affect the inner application as well.
Based on the fact that your applications are sharing common binaries already, you may want to look into using MVC Areas to facilitate a logical separation between applications (but have them all as part of a single application). The following link describes this in more detail: MVC Areas

Directory listing error on published uploaded MVC3 app

Ok so I uploaded an MVC3 application onto a server. The server is .net enabled. However when I go to the directory its under i am presented with this error:
Directory Listing Denied
This Virtual Directory does not allow contents to be listed.
I made sure that:
System.Web.Mvc
System.Web.Routing
System.Web.Abstractions
were set to copy local true.
I feel as if it may be because of two issues. Firstly it may be an IIS issue, which will require reading about virtual directory permissions.
or its because my project hasn't got what is effectively an index page. At the directory the layout is as follows.
bin(folder)
Content(folder)
Models(folder)
Scripts(folder)
Views(folder)
Global.asax
packages.config
Web.config
First MVC3 app Ive uploaded to a server, anyone know where im going wrong, or which one of the issues they think it is? If it is either of them?
Either add runAllManagedModulesForAllRequests=”true” in your web.config or apply QFE on your web server. More details in How ASP.NET MVC Routing Works and its Impact on the Performance of Static Requests
Ok, I'll give you a check list that might help you out, or at least point you in the right direction.
Have you installed everything for .Net and MVC to work? MVC might work just deploying /bin folder. Installing the RTM MVC3 package will probably save you some IIS/Site configuration.
Run this, just in case... aspnet_regiis -i.
Target your brower to a know page from your project, for explame /Home/Index. Remember the MVC routing stuff, if you try something like "http:/yoursite/views/controller/action" you won't get the correct response. Now, if you get an error here, it will tell you something useful, like an Authorization error.

Categories