single page application (SPA) home page error - asp.net-mvc

I am new to asp .net MVC. I am trying to implement SPA model for my new application. I am using asp .net web api in asp .net mvc4 project. I have only api controllers in my project. I dont have any MVC controllers.
I have deleted auto generated views from the views folder. I have created a index.cshtml page outside the views folder. This page is my layout page that renders other pages in it. I have set this page as startup page. And also i have commented out the default MVC route from routeconfig.cs file.
The problem is when i run the application, the index.cshtml does not render. I get an error saying "This type of page is not served - Description: The type of page you have requested is not served because it has been explicitly forbidden. The extension '.cshtml' may be incorrect."
Please help

Make sure you keep the root Views folder, and create a folder called Home within that. Place your Index.cshtml file within the Home folder.
Do not comment out the default MVC route. In MVC, you can't hit a view directly, as you do in WebForms, which is why you get the error when you try to hit Index.cshtml.
Make sure you have a HomeController within the root Controllers folder, with an action called Index that looks like this:
public ActionResult Index()
{
return View();
}
Your view should now render when you browse http://localhost/Home/Index or even just http://localhost as by default, the controller and action will be set to Home/Index.

Related

ASP.NET MVC 5. The resource cannot be found error on navigating to AreaName/Home/Index

I converted a ASP.NET 4 WebForm website project from VS2010 to a ASP.NET 4.5 Web application project in VS2013. Then I created an Area in this new project and copied the Controllers, Models and views files from another VS2012 MVC4 project to this new Web App project. The application works fine when I navigate to the WebForms pages. But when I navigate to the MVC views, I get the following error The resource cannot be found localhost:1234/AreaName/Home/Index. I have the Home controller and Index action method and Index.cshtml. It works in the VS2012 project but there is no Area there. My 'AreaNameAreaRegisteration.cs file has:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"AreaName_default",
"AreaName/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
EDIT
I just did the following: Added a new controller called 'JustTestController', right clicked on its Index action method, clicked on 'Add View', it created a view Index.cshtml. Now when I browse to http://localhost:1234/AreaName/JustTest/Index, the view is displayed. So the question is why when I copy/paste the conrollers/models/views from another project (created in VS2012) those views give the above error even though manually creating a controller and corresponding views of its actions do display without error. Is there something I need to do for those controllers/views that I copied/pasted from the VS2012 project?
Issue Resolved
I have resolved the issue (thanks to Erik). Please see my comments below.

How to use [HandleError] without Master Page

I am trying to use [HandleError] in MVC to redirect any un-handled exceptions to the "Error.aspx" page in the "Shared" folder. By default the Visual Studio 2010 MVC2 template creates a simple MVC application with this function. It works fine.
But the Error.aspx file created by default uses "Site.Master". But I do not want to use master page in my application. I deleted default "Error.aspx" and added my own. But it simply does not work. Anyone has any idea on how to use [HandleError] without Master page?
Thanks ...
Put your custome Error.aspx or your custome error page in shared views
[HandleError(View = "Error")] // Or your custome error page name

working with an .aspx page with in asp.net mvc 2

I'm new to asp.net mvc.
I just created the default asp.net mvc project in VS and I can see that when I make a call to a controller's action like this: "http://localhost:2528/Home/About" my Home controller has an action method "About" that is returning the about.aspx view. however I am seeing not seeing the .aspx extension in the browser's url. And when I try to browse to "http://localhost:2528/Home/About.aspx" i get a 404 error.
I have a requirement where I need to create a .aspx page that is passed an arguement via the url like this: "http://... /myAspxPageHere.aspx?argName=myArg"
I'm not sure how to do this with asp.net MVC. Any help and/or code examples would be greatly appreciated.
Thanks
ASP.Net MVC does not use the .aspx extension in client-facing URLs.
If you want to accept arguments from the querystring, you should add parameters to your action methods.

Default.aspx page in asp.net MVC

What is the Default.aspx page for in an MVC app? Does this actually execute? Does this page have anything to do with the "Default" route in the global.asax?
If you take a look at the comments at the top of Default.aspx
Please do not delete this file. It is
used to ensure that ASP.NET MVC is
activated by IIS when a user makes a
"/" request to the server.
Basically, it acts as an override for the possibility of a different default page being loaded by IIS. In IIS6 if you have an index.html in the same directory as your MVC application, it can sometimes render that file instead of looking for the default blank route in your application. Default.aspx prevents that from happening.

How do I set up a route for the home page of an ASP.NET MVC site?

I'm working with an ASP.NET MVC site which will use a CMS controller for all pages of the site except for the home page. Here's the idea:
Home controller:
www.site.com
www.site.com/default.aspx
CMS Controller:
www.site.com/about
www.site.com/agenda/schedule
www.site.com/monkey/eats/spaghetti
(pretty much anything else)
This page lists some options on how to set up a default page routing:
Leave Default.aspx unrouted and unredirected as the entry point to your application - with static links that take your users into the MVC portion of the app (or other static content).
Redirect Default.aspx in the code behind, either using the Page_Load event handler code, or use Response.Redirect("~/home") to send them to the Home controller (although this is a round-trip redirect).
Rename or delete Default.aspx. Despite the warning in the markup that says that default.aspx is required to ensure that ASP.NET MVC is activated by IIS when a user makes a "/" request... it's not actually needed in either the VS dev server, or IIS7. The default request will remain an application root request "/" and will be caught by the default route and sent to the home controller.
I guess one other option is to just use one controller with some logic that detects the home page case, but that seems to be fighting the concept.
How do you recommend setting up a specific route for the site home page?
www.site.com can be handled by an root map route
routes.MapRoute(
"Root",
"",
new { controller = "Home", action = "Index", id = "" }
);
Put the following in page load of Default.aspx
HttpContext.Current.RewritePath(Request.ApplicationPath, false);
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest(HttpContext.Current);
This rewrites the request to root and handled by the map route above.
BTW, you can actually find the code from the MVC template project.
If hosting on IIS7 integrated mode, I suggest just getting rid of default.aspx. As I understand it, it's only necessary for activation on IIS6 and IIS7 classic mode.
I think option #1 is easiest. I probably will stick to it until finding a strong reason to move or finding the alternative. The default template uses this approach.
[UPDATE] Canton beat me to it

Resources