Why can't MVC find my view correctly? - asp.net-mvc

I'm working on MVC5.
I already have my controllers and their respective views. If I click on a view and open it on my browser, everything's fine, however, whenever I start the project normally on VS, my browser opens, for example, this link:
http://localhost:50738/Views/Profile/Index.cshtml
However, whenever I open the view directly I have:
http://localhost:50738/Profile or http://localhost:50738/Profile/Index
At the RouteConfig file I just said that I wanted Profile to be shown by default, instead of home. Why does '/Views/' appear on my browser?
code:
namespace WorkTimeManager.Presentation
{
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 = "Indicators", action = "Index", id = UrlParameter.Optional } );
}
}
}

Its because you have your Start Action set to current page in your web project. So when you start your web app it will try to load the page you are currently viewing in Visual Studio.
To fix this, right click your web project in your solution and click properties, then click on to Web and you will see Start Action - with Current Page radio button selected.
Change this to Specific Page and then type in your homepage URL e.g. http://localhost:50738/Index then everytime you start your web app from Visual Studio it will open on that page instead of trying to open the current page.

When you click on a .cshtml file ("click on a view"), you're only opening the file that describes that view, you're not opening it via 'mvc'.
MVC doesn't send the .cshtml file to the browser, the controller reads the view and renders it.
So the file in Views/Profile/Page.cshtml may be rendered by a controller action ProfileController.Page() (or may not... but would be in the simplest case) for which the URL would be /Profile/Page
This is a brief summary, there are better explanations available of how MVC works.

Related

Create a new cshtml page in MVC project

I have a MVC website project to design it , I don't have any knowledge about MVC coding :( . All I want to do that create new .cshtml page in (views/home) folder, which will be welcoming page (or new home page), and display this page before the existing page (home/index) which will open by link or button in my new home page.
Please, I want to knew method.. step by step .
Note: the developer gave me a beta link of site which don't contain a many folders like (Models , Controllers , App_Start , ...).
I hope It's clear,
Thanks in advance. :)
first you can create new Action in Home controller. Right click on this Action and create view for this ActionResult Newpage.
public ActionResult NewPage()
{
return View();
}
change RouteConfig.cs file under App_Start folder like this
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "NewPage", id = UrlParameter.Optional }
);
now build your application and run your project. Do you learn more about MVC CRUD?
You can change the default homepage in App_Start > RouteConfig.cs.
Change its controller and action to your welcoming page.
In your welcoming page, just set up a button that redirects the page to your homepage.

Visual Studio 2012 asp.net mvc default controller/a not working

hello I am developing an Asp.net MVC 4 app with Visual Studio 2012 ultimate updat4 every time I hit run the browser show opened View instead of default controller which I set in RouteConfig and the same project works perfectly on Visual Studio 2013.
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 }
);
}
You can change default page from Project Properties. Follow below steps:
Right click on Project in Solution Explorer, and select Properties.
Select Web tab in Properties Window.
Now change Current Page selection to Start Url, and paste your project url here. like http://localhost:51774
And then save.
After this, when you run project, default page will open in browser with your project url.
In mvc you can not directly access your .cshtml File. Try following url
localhost:51774/Home/Transaction

debug loads the currentt view instead of the view specified in routeconfig Mvc4

I have been working on a mvc4 solution and up until now when I pressed the debug button, the browser would open up to the /Home/Index action. no matter what class or view I was working on, it would load the default, just as specified in routeConfig.cs
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 }
);
}
since this morning this has changed. now if I am currently working on /Home/Example and press debug, the browser opens to that instead of Home/Index.
I have no Idea what configuration I changed or what is causing this. any suggestions are welcome.
Your problem has nothing to do with routing. It is related to the default startup page in Visual Studio.
Right click on the ASP.NET MVC project in your solution explorer and choose Properties. Then navigate to the Web tab and on Specific Page write Home/Index:
Maybe in your project you've got Current Page selected.
Did you remove your *.suo files? These files contain information related to the properties of the web application. You can simply right click your web app project in the solution explorer and select the properties menu item. On the properties page select the web tab on the left tab strip and select the radio button labeled "Specific Page" and leave the text box empty. This will ensure your web is always loaded at the root url during debug.

ASP.Net page flow

I new to using ASP.net-mvc and would like some help. I created a web application using VS2010 and need to change the way the pages flow. How do I code it so that the first page the user is presented is not the home page but the login page?
You modify the default route in Global.asax in the RegisterRoutes method.
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional }
);
You need to change your route (in global.asax.cs) defaults - from controller "home" and action "index", to what you need.
And more importantly you need to read some tutorials and books on technology your are new on, before asking basic questions on SO.
From Stephen Walther's page:
Specific Page – Enables you to set a particular page to run. You can set the page here or you can right-click a page in the Solution Explorer window and select the menu option Set As Start Page.
I have not tried this with mvc 3 app however.

ASP.NET MVC Routing in Azure

I have an Azure Web Role project that was recently MVC'd by another developer. According to the developer the app works with no problem when run on it's own (i.e. as a simple web app). However, when I try to run it in the context of the Azure cloud service, I'm seeing a number of 404 errors. I suspect something is not quite right with the routing. Here's an abbreviated version of the current RegisterRoutes method that's part of Global.asax.cs:
public static void RegisterRoutes(RouteCollection routes){
routes.IgnoreRoute("{Services}/{*pathInfo}");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Configuration",
"Configuration",
new { controller = "Configuration", action = "Index" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Account", action = "Index", id = "" }
);}
When the app starts up, the correct view from the Account controller's Index action is displayed. However if I try to navigate to Configuration I get a 404. Converesly if I change the method to this:
public static void RegisterRoutes(RouteCollection routes){
routes.IgnoreRoute("{Services}/{*pathInfo}");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Account",
"Account",
new { controller = "Account", action = "Index" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Configuration", action = "Index", id = "" }
);}
I get the correct view from the Configuration controller's Index action, but I can't navigate to the Account view.
I'm guessing this is a simple problem to solve, but not knowing what exactly was done to "MVC" the Azure app and being new to MVC has me beating my head into the wall.
Here's the configuration of the machine where I'm encountering this issue:
Windows 7 Ultimate with IIS 7.0
Visual Studio 2008 SP1
ASP.NET MVC 1.0
Windows Azure SDK 1.0
Thoughts?
Try using my Routing Debugger. It can help you understand what's going on. http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
It's weird that the behavior would be different locally than in Azure. Also, you should post your controller code (remove the contents of the action methods, we just need to see the method signatures).
If I had to make a wild guess, I'd guess your Configuration route (in the first example you gave) needs to add id="" in the defaults section.
Haacked: Thanks for pointing me to the debugger. That helped me hunt down the issue in a matter of minutes.
The answer was much simpler than I thought. It all had to do with the following line of code:
routes.IgnoreRoute("{Services}/{*pathInfo}");
I put this line in to help resolve an issue I was having with ASP.NET MVC and WCF RIA Services (more info on that here). The curly braces shouldn't be there. I don't want to replace Services. The code should look like this:
routes.IgnoreRoute("Services/{*pathInfo}");
You can read a full write-up here.
I don't think this is your problem, but you might verify that the System.Web.Mvc reference has its Copy Local = true.

Resources