MapRoute in mixed web forms/MVC app - asp.net-mvc

I have an older web-form app and now I am trying to add new pages using MVC.
It all seems to work just fine except one thing:
The application's default page (login.aspx) is a web form.
When user hit link www.mysite.com, instead of opening www.mysite.com/login.aspx, the site immediately goes to route specified in global.aspx as
routes.MapRoute("FrontLine", "{controller}/{action}/{id}",
new { controller = "FrontLine", action = "QuickView", id = "" });
So, the question is how to make login.aspx a default page?
Is it possible to do without converting login.aspx to a MVC view and adding corresponding controller?

Found a solution:
excute line:
routes.MapPageRoute("Default", "","~/Login.aspx")
before calling
routes.MapRoute

Related

Default page for MVC application

As far as I know, which isn't a lot, there is no way to set a default page, like Default.aspx for an MVC 4 application, right? I am developing MVC apps in convert with developers still working with Web Forms, which allows them create a default page. In other words they have a Default.aspx page in their root folder and when the user enters http://www.example.com it will display that page. We have a requirement to display some standard stuff before allowing the user to continue in any of the apps. So would I create a Default.aspx or Default.cshtml file and use this as the initial display page then call another controller and action after the user agrees to the terms? What's the best way to do this?
In MVC, you can set default route in RouteConfig.cs as
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
When the user first visits your page using www.example.com, the user will be redirected to Home/Index where you can place your terms and conditions. But, this might not be always the case as the user can navigate to any URL and you need a mechanism whether a user has accepted the terms and conditions or not. I would suggest putting a cookie or session variable saying the user has accepted the terms. Check whether the cookie or session exists or not. Otherwise redirect to your terms and conditions page. On terms and conditions page, don't check any cookies. You can check that cookie using a number of ways:
1) Using ActionFilterAtribute and overriding OnActionExecuting
2) Using HttpModule
3) Overriding Application_BeginRequest() inside Global.asax.cs

Changing URL without changing Actual Path to Redirect

I am new to ASP.Net and working on MVC 4. I want to replace my current URL with a customized URL.
For example:
Current URL: http://www.testsite.com/home?pageId=1002
Desired URL: http://www.testsite.com/1002/home/
So the URL that is displayed in the address bar will be the desired one and actual URL working will be the current one.
I have tried URL routing in Global.asax file of my project but doesn't seems to be working for me.
What exactly I want is to put the URL Like this.
Thanks in Advance.
ASP.NET MVC 4 provide a toolbox way to write your application. The URL that you see in the browser comes from Routing that do the hard work to convert url to app routes and app routes to url.
1) The default ASP.NET MVC 4 Template project comes with a file at App_Start folder named RouteConfig, where you must config the routes for the app.
2) The routes has precedence order, so, put this route before the default one:
routes.MapRoute(
name: "RouteForPageId",
url: "{pageId}/{action}",
//controller = "Home" and action = "Index" are the default value,
//change for the Controller and action that you have
//pageId is the parameter from the action that will return the page
defaults: new { controller = "Home", action = "Index" }
);
Now you can enter myappdomain/1220/index for exemple.
Hopes this help you! Take a look here for more info ASP.NET Routing!

Help with MVC view

When I create new MVC 3 project in Visual Studio the first thing I do is create a new controller called Home, and then right click in the Index stub and create a new view.
My question is when I have the Index file selected in the solution explorer and build the solution I get an error saying it can't be found. If I navigate to the page using root/ Home or root/Home/Index it still doesn't work.
I also created a new project using the sample website that ships with MVC and cannot figure out what code differs between an empty solution and the sample solution that could be giving me this problem. In the global.asax it looks like there is already a route setup for a home controller so I'm confused.
Your controller class should be called HomeController, not Home. On the screenshot you've shown I see that you've called it Home. By convention all controller classes in ASP.NET MVC must have the Controller suffix. In Global.asax you should have a default routing rule which states:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
which means that when you run the site and you request / it will look for the Index action on the HomeController.
I am unable to reproduce with Microsoft Web Developer 2010 Express, ASP.NET MVC 3 and creating a new empty Razor-view project. When you choose "Add View" did you enable "Create as a partial view"? If so, that may be the issue. Did you enable "Use a layout or master page" and select an existing master page? If not, that may be the issue...
The routing looks fine in an empty project. I see the Home/Index view with these URLs:
/
/home
/home/index
Post problematic project as .zip?

Strange route problem in ASP.NET MVC - default route not hit

I have this two routes currently in my application after decommenting out many other ones. Let me first explain that I have quite a big application already but have come to a problem where my application does not start at the root url anymore.
If I set starting page to default.aspx then webapp starts at (example) http://localhost:55421/Default.aspx. I don't want that. I want it without Default.aspx
So I went into app properties and removed Default.aspx as starting page - now it is blank field (just like in a sample new MVC app if you create it in VS 2008).
But now application does start at the required URL but issues an error:
"The incoming request does not match any route."
Also If I use route debugger it also misses all routes and catches it by catchall route.
I don't know how all of this is possible since as I said above I have two default routes configured at this time:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Pages", action = "Display", slug = "Default" }
);
Any help appreciated
Am I right in thinking you are trying to hit
http://server/{controller}/{action}/{id}
with
http://server/
If you are I think you need to provide a default for the last parameter {id}. You have a default for a parameter slug but without a default for {id} I don't think ASP.NET Routing can hit it.
If I'm right
http://server/Pages/Display
should also not hit the default route, because you are expecting id in Display?
HTH
Alex

How do you set the startup page for debugging in an ASP.NET MVC application?

How do you start debugging the application at the application root? For example: http://localhost:49742/
I'm always getting a page which doesn't exist, such as:
http://localhost:49742/Views/Home/About.aspx
Note that it would be OK to start at http://localhost:49742/Views/Home/About
Go to your project's properties and set the start page property.
Go to the project's Properties
Go to the Web tab
Select the Specific Page radio button
Type in the desired url in the Specific Page text box
While you can have a default page in the MVC project, the more conventional implementation for a default view would be to use a default controller, implememented in the global.asax, through the 'RegisterRoutes(...)' method. For instance if you wanted your Public\Home controller to be your default route/view, the code would be:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Public", action = "Home", id = UrlParameter.Optional } // Parameter defaults
);
}
For this to be functional, you are required to have have a set Start Page in the project.
This works for me under Specific Page for MVC:
/Home/Index
Update: Currently, I just use a forward slash in the "Specific Page" textbox, and it takes me to the home page as defined in the routing:
/
Selecting a specific page from Project properties does not solve my problem.
In MVC 4 open App_Start/RouteConfig.cs
For example, if you want to change startup page to Login:
routes.MapRoute(
"Default", // Route name
"", // URL with parameters
new { controller = "Account", action = "Login"} // Parameter defaults
);
If you want to start at the "application root" as you describe right click on the top level Default.aspx page and choose set as start page. Hit F5 and you're done.
If you want to start at a different controller action see Mark's answer.
Revisiting this page and I have more information to share with others.
Debugging environment (using Visual Studio)
1a) Stephen Walter's link to set the startup page on MVC using the project properties is only applicable when you are debugging your MVC application.
1b) Right mouse click on the .aspx page in Solution Explorer and select the "Set As Start Page" behaves the same.
Note: in both the above cases, the startup page setting is only recognised by your Visual Studio Development Server. It is not recognised by your deployed server.
Deployed environment
2a) To set the startup page, assuming that you have not change any of the default routings, change the content of /Views/Home/Index.aspx to do a "Server.Transfer" or a "Response.Redirect" to your desired page.
2b) Change your default routing in your global.asax.cs to your desired page.
Are there any other options that the readers are aware of? Which of the above (including your own option) would be your preferred solution (and please share with us why)?

Resources