ASP.Net page flow - asp.net-mvc

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.

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.

Error 404 mvc- area rooting

I have a problem with an asp.Net project. I have changed some things in my client. At first the register view was inside the account view by default but i wanted to change that because i did other changes in the code too. Now when i run it can't find the path(error 404) .I changed the view by clicking at my register controler => add view. I have read other posts about the mvc areas but i can't unterstant exactly what i need to change because i am a newbie. I tried that code. My controller name is register my method ActionResult is also register().
routes.MapRoute(
"Register", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Register", action = "Register", id = UrlParameter.Optional });
Your Register action method exists in the RegisterController now, not AccountController. So you need to update the calls to your old register action method (Account/Register) to the new one (Register/Register), in your layout / other views files
Change
#Html.ActionLink("Register", "Register", "Account")
to
#Html.ActionLink("Register", "Register", "Register")
This will generate Link with href value set to "Register/Register" which is a valid request for an existing route.
The bit of code you referenced is exactly the same as the default rule, except that if you enter a url with no path it will assume it should use a controller named Register using an action named Register. It will actually overwrite your default rule if it is placed first because all possible URLs match the URL pattern.
localhost:59436
localhost:59436/Register
localhost:59436/Register/Register
These all point to the same thing using your Routing rule. I'm not sure that is what you intended.
What is the name of the controller and action you are using to register? You need to update your links to use:
#Html.ActionLink("Register", "ActionName", "ControllerName")

How to set a startup page in Expression Blend for an MVC project?

We are hosting a Silverlight application inside an ASP.NET MVC view. There isn't an aspx or an html page that we can choose as the startup - we need to launch the application using a URL.
It seems that Expression Blend doesn't allow this startup configuration - you must choose a specific page. As a result you can't start/debug the application from Expression Blend - it complains about startup page not being set.
Is there a solution/workaround that would allow us to start an ASP.NET MVC-hosted Silverlight application from Blend?
What I have been doing, is to create a URL route (in your ASP MVC Global.asax.cs) that catches whatever page your Silverlight application is trying to hit. Instead of returning that .aspx page or a 404, it should hit the controller and return your view that displays the silverlight app (assuming the route is set up correctly).
For example,
// route to catch Silverlight test page
routes.MapRoute(
"SilverlightTestUrl",
"Project.SilverlightTestPage.aspx", // whatever page it's trying to start
new { controller = "Home", action = "Silverlight" }
);
// make sure the silverlight route is before the default 'catch all' route
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Silverlight", id = UrlParameter.Optional } // Parameter defaults
);
EDIT: although sorry, I guess this doesn't help the issue of being able to run the project from Expression Blend. I'm not a big fan of blend, I don't know why they didn't jsut incorporate some of the features from Blend into Visual Studio. Visual Studio's XAML editor is much better than blend.

Asp.Net MVC Default Route

I have my default route defined like this
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
However if the user is logged in when they visit the site (This can happen if they ticked the remember me button last time the logged in) I want them to take a different default route and go straight to the logged in page.
Is this possible in global.asax or will I need to put some logic in my home controller to redirect if logged in?
Best to put this in the home controller. A check if authenticated and return the appropriate view.
I want them to take a different default routeRouting in ASP.NET MVC is about routing URLs to action methods on controllers, not about routing users to places in your web site depending on the current circumstances. (Think of routing as a static thing, whereas the rest (authorization, redirection, etc) is only applicable to the current session.)
It is possible to use Routing Constraints to achieve what you want, but I don't think that's what you want.

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