Host Asp.Net MVC in IIS hides Views-Folders and .cshtml - asp.net

When I host my Asp.Net MVC inside my default IIS, it does neither show .cshtml files, nor the Views folder at all. Even when directory browsing, these folders are missing. If I create some file in the Views folder I cannot access it either. If I create some file in another folder I can access it.
This is my routing configuration
I think this should be a rather basic setup question. Do I have to manually add MVC support?
My IIS is running in my docker container on the basis of windowsservercore. Setup as follows:
FROM microsoft/windowsservercore
SHELL ["powershell"]
RUN Install-WindowsFeature NET-Framework-45-ASPNET ; \
Install-WindowsFeature Web-Asp-Net45

Routing doesn't go to views it goes to controllers. It is the controllers that process and then render the views. Additionally, there is no physical correlation between the URL that you provide and the view location - that is entirely based on how the routes are setup.
It is normal and expected behavior for IIS to block direct access to the Views folder, since these are never used by the browser directly.
The Views are convention-based by default. The convention they use is Views\<ControllerName>\<ActionName>.cshtml. When a view is in the correct corresponding location, the action method can simply return View(); and the view will be rendered.
The view Views\tst.cshtml will only be rendered if you have passed the virtual location of the view to the View method (for example, return View("~/Views/tst.cshtml");. This is because it does not match any of the "normal" MVC conventions.
Since you have not provided any examples of what your controller or action names are (or even if you have any), I will provide a basic example from the MVC template project.
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
With the above controller and the Default route, if you pass the URL /Home/About (Home matching HomeController, and About matching the About method), the About method will run, and the view at \Views\Home\About.cshtml will be rendered.
Reference: Understanding Models, Views, and Controllers (C#)

Related

IIS server error .Server Error in '/' Application

This is an ASP.NET MVC app but using the Web Forms View Engine (.aspx) instead of the Razor View Engine (.cshtml).
I've set the default start page to Login.aspx in the project and in IIS default document. However if i enter http://localhost:150/Login.aspx to the web browser it works. What am I doing wrong?
If you want to access to the route '/' you should have a HomeController. Inside this controller, put an Index action that returns View(). Then create a Index.aspx file in the Views/Home folder. If you want access to other view when you enter to your site with route '/', then you should change your RouteConfig file, and set the default controller and action to others.
I have figured it out. I moved my login page into the VIEWS folder ( it wasn't before).
Created a default controller
public class DefaultController : Controller
{
// GET: Default
public ActionResult Index()
{
return Redirect("~/Login.aspx");
}
}
And not everything seems to work. Thank you for your assistance.

ASP.NET Web.Api plugin architecture

Can you suggest me some articles or code samples about plugin architecture in web api?
Currently I'm thinking about this scenario: to have 1, centralized api gateway, where every client sends request, and have different applications controllers in Plugins folder. If someone wants to add new service, writes it's own controllers and puts dll files in Plugin folder.
For locating controller classes at run time, you can write an assembly resolver, like this.
public class MyAssembliesResolver : DefaultAssembliesResolver
{
public override ICollection<Assembly> GetAssemblies()
{
List<Assembly> assemblies = new List<Assembly>(base.GetAssemblies());
// Add all plugin assemblies containing the controller classes
assemblies.Add(Assembly.LoadFrom(#"C:\Plugins\MyAssembly.dll"));
return assemblies;
}
}
Then, add this line to the Register method in WebApiConfig.
config.Services.Replace(typeof(IAssembliesResolver), new MyAssembliesResolver());
With this, the request will still need to be sent to the individual controller even though the controller classes can come from assemblies in the plugin folder. For example, if MyAssembly.dll in the plugins folder contains CarsController, the URI to hit this controller will be /api/cars.

Adding asp.net.mvc to ASP.NET: Controllers folder

I have an ASP.NET application, and I am trying to turn this into a hybrid ASP.NET / ASP.NET.MVC 4.0 application.
I tried to create a folder named "Controllers", and place a .cs file in there:
public class PlayerGroupController : Controller
{
public PlayerGroupController()
{
}
public string Index()
{
return "Hello World!";
}
public ActionResult LayoutTemplates()
{
return View();
}
}
Any attempt to access "PlayerGroup/LayoutTemplates" doesn't work (just get a "Not Found" error)
I then moved this file into App_Code, and it works fine. I'm glad I got something working, but I would rather follow the convention of controller classes being in the folder named Controllers.
Is there some magic setting I can set somewhere so that it starts recognizing Controllers as a code folder?
If the project is configured as a Web Site Project then all code files (*.cs) must be in ~/App_Code or in subfolders thereof. If the project is a Web Application Project, code files can go anywhere in the project and be compiled by VS into a DLL that ends up in the ~/bin directory, which then gets loaded by ASP.NET.
MVC is geared specifically towards the Web Application Project (WAP), so I recommend creating a new WAP and copying all the files into that, and then going from there.

How do I redirect to the cshtml page created inside shared folder in ASP.NET MVC3

Please help me out how do I redirect to a .cshtml page which is created inside a shared folder.
I want something like this
href="#Url.Content("Shared/UnderConstruction")"
Here it's not getting redirecting to the UnderConstruction page, which I created.
You cannot redirect to anything that's stored inside the ~/Views folder including the ~/Views/Shared. This folder is simply not served by ASP.NET MVC and cannot be accessed directly. Also notice that in ASP.NET MVC you are not serving .cshtml pages directly. You are always passing through controller action that return views and those views might represent .cshtml pages.
Now if you have some Shared folder directly under the site root ~/Shared, then you can do this:
construction
In ASP.NET MVC3 you can't render views directly by calling the files directly. They can only be served via controllers.
In order to call the view in your shared folder you woul have to do something similar to the following:
public class HomeController : Controller
{
public ActionResult About()
{
return View("Construction");
}
}
If you want to display a page at url "shared/underconstruction" as per the other posts:
Create controller SharedController.
Define action "UnderConstruction"
Create "UnderConstruction.cshtml" in Views/Shared/ folder.
Map URL "Shared/{action}" , new { Controller = "Shared" } if you want to be explicit.
Give that a shot... to be honest even I don't know if this will work, and you will pollute your "Shared" folder. You could rename existing Shared folder to something else, maybe, and modify ViewStart.cshtml to point to new folder, maybe.
In ASP.NET MVC you can only redirect to controllers, and the controllers return a view. You can access views in Shared the same way as your normal controller views, by their name. ASP.NET MVC first looks in your controller view folder then in your shared view folder when resolving view names.

protecting files with asp.net (mvc)

I want to protect the files in a specific directory so that people cannot link to them - but I still need my program to be capable of rendering them, so password protecting the actual folder won't work. Is there any way to facilitate this in ASP.NET MVC? Obviously it's more about ASP.NET, not MVC - but I can't make use of things like Session State and Postback and such...
You could put that directory outside of the web app's root directory (so that it can't be accessed using some copied URL) or into a directory where you deny any read access using a web.config file.
Then access the files through an action which requires the current user to be authorized, e.g:
public class FileController : Controller
{
[Authorize]
public ActionResult Get(string file)
{
return new File(Path.Combine(_rootPath, file);
}
}
Then you can access the files through an action URL, e.g. http://server/app/File/Get/test.txt.

Resources