IIS server error .Server Error in '/' Application - asp.net

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.

Related

All href in html produces 404 error when using APS.NET MVC

I just migrated all my html code to APS.NET Core, but all links to other pages failed and give 404 error, for instance this bookmark links to the file Companies.cshtml which is in the same View/Home folder.
<div>
foo
</div>
I tried to add folders to the workspace with browser inspection but no avail.
I also tried add ~/ to the path, does not work.
I've noticed that the localhost cannot find the file Companies.cshtml in a virtual file system(?) and thus may be the reason that produces the error. How may I bypass the issue?
You cannot link to a view. You need an action that loads that view, and then you link to the route that directs to that action.Read Routing to controller actions in ASP.NET Core to know more.
For example:
You can create an Companies action in HomeController :
public IActionResult Companies()
{
return View();
}
then use
<div>
foo
</div>
result:

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

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#)

ASP.net "{Controller}/" returning 403.14 error

I am having a curious issue with one of my projects in development. The issue is with links to a certain URL "//localhost:62168/Images/Index". I have buttons linking to that URL but when "//localhost:62168/Images/" is accessed it returns a HTTP Error 403.14 - Forbidden error. See the below Image:
localhost//Images/
Oddly enough though, when I enter the URL exactly ("localhost:62168/Images/Index") it loads the page properly. See the below Image:
localhost/Images/Index
I've done plenty of research online and I believe it may be an issue with routing so below I've added the code of my "RouteConfig.cs" file. Unfortunately, I am new to ASP.net and MVC and not knowledgeable on proper Routing procedures:
using System.Web.Mvc;
using System.Web.Routing;
namespace ReedHampton
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
I've also tried multiple other "solutions" presented in other online forums to no avail. These include:
Adding "directoryBrowse enabled="true" " to web.config
Running "C:\windows\Microsoft.NET\Framework64\v4.0.30319> aspnet_regiis.exe -i" in Command Prompt
Going through IIS Express and registering IIS Express and ASP.Net
I would greatly appreciate any help that anyone can provide!
IIS will attempt to short-circuit the request if it finds something on the filesystem that matches the URL. In other words, I'd assume you have an Images directory in your document root. Therefore, IIS will attempt to hit this directory, rather than pass the request on to the ASP.NET machinery. Since you've disabled directory browsing, you get a 403.
Long and short, you need to keep your ASP.NET MVC routes unique from what you have physically in your document root. You could change the name of the Images directory to something like img, or put it in a parent folder like the default of /Content/Images. Otherwise, you'll need to change your controller name or create a custom route to that controller not called /Images.

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.

ASP.NET MVC thinks my virtual directory is a controller

I have a virtual directory under my MVC website in IIS called "Files". This directory is at the same level as my Views directory. When I link to a file from my MVC app to a file under my Files directory, I get the following error:
The controller for path
'/Files/Images/1c7f7eb8-5d66-4bca-a73a-4ba6340a7805.JPG'
was not found or does not implement
IController.
It thinks that my Files VD is a controller. How do I access my files like a normal VD without MVC interfering?
Thanks.
When registering routes, try to add the following Ignore rules.
public static void RegisterRoutes(RouteCollection routes)
{
/* Ignore static content, see
http://weblogs.asp.net/rashid/archive/2009/04/03/asp-net-mvc-best-practices-part-2.aspx
*/
routes.RouteExistingFiles = false;
routes.IgnoreRoute("Content/{*pathInfo}");
routes.IgnoreRoute("Scripts/{*pathInfo}");
routes.IgnoreRoute("Styles/{*pathInfo}");
routes.IgnoreRoute("{*favicon}",
new { favicon = #"(.*/)?favicon.([iI][cC][oO]|[gG][iI][fF])(/.*)?" });
//Ignore handlers and resources
routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// your routes go here
}
ASP.Net looks for the directory first and then tries to match a controller, so what you are doing should work. Are you sure the file with that name exists and is accessible?
I think you'll have to add a call to routes.Ignore() a static route in your Global.asax file so that .NET MVC knows to ignore the request:
RouteCollection.Ignore(String) - MSDN

Resources