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
Related
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.
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.
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.
I have a MVC 3 Razor app and I need to deploy it on http://www.mydomain.com.
The trouble is that that, when I hit http://www.mydomain.com it gives a 404 Error. I need http://www.mydomain.com/Home view loaded by default, I would like to avoid using a redirect method as it is not SEO friendly...
If you haven't chanegd the RegisterRoutes in the Global.asax.cs file, then it defaults to /Home/Index
see below.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
So the 4th line of code is where the defaults are configured.
As for the default setting, you can use the RegisterRoutes(RouteCollection routes) method that is available in Global.asax.cs to mark the default controller and its corresponding action method. Regarding hitting www.mydomain.com and getting a 404 error, I need more information. if you are on windows and working on a developer machine, did you add the entry to hosts file located in
C:\Windows\System32\drivers\etc
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.