mvc forms authentication landing page - asp.net-mvc

I have an MVC website that uses Forms Authentication.
I am attempting to add a default landing page to my site ( landing.html ) that each user hitting
http://mywebsite.com for example will be redirected to.
I tried setting the default document in the web.config, but I always get redirected to the Account/Login page.
I do not want to change the login url because that will make the user's whose session has expired to go to the landing page, which is not what I want.
Any help would be appreciated.
Thanks

Update your "default" route. MVC projects have the following route defined in the RouteConfig class and is called in the Global.asax during application startup.
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 = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Just replace "Home" and "Index" with the controller and method you want to use as your default landing page.
Also be sure that if the controller has the [Authorize] attribute added to it then you need to add the [AllowAnonymous] attribute to the method you want to use.

Related

Use of rewritten URLs vs direct controller call

Is there a simple catch-all way to ensure only rewritten URLs can invoke a controller?
For example, if we have a URL www.somesite.com/about pointing to action "About" in controller "Shared", can it be ensured that any requests to www.somesite.com/shared/about end up at the rewritten URL, in this case www.somesite.com/about?
In other words, the user should not be able to just type /controller/action without being redirected to the rewritten URL.
However, we don't want to actively check and redirect but were hoping for some built-in function of MVC. The only suggestions I found along those lines were ChildActionOnly and HttpPost attributes, but they don't seem to be the answer (normal links don't work).
As mentioned, we're looking for something simple, more or less built-in - if it doesn't exist then so be it...
The built-in way of blocking routes is to use IgnoreRoute. It short-circuits routing and always makes the path throw a 404 not found.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Ignore /Home/About
routes.IgnoreRoute("Home/About");
// Register /About
routes.MapRoute(
name: "About",
url: "About",
defaults: new { controller = "Home", action = "About" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Note that under the covers, it uses the StopRoutingHandler, which can be used (as a replacement for MvcRouteHandler) in any custom Route or RouteBase implementation to make more dynamic ignore rules than this.
NOTE: It is extremely important that IgnoreRoute is registered before the route you want to ignore in the route table.

I want to call controller's Default action.

Example : My User will enter www.xyz.com/Promo/PROMO123
where "PROMO123" is value, which i require.
above code produces error :
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
However
www.xyz.com/Promo/Index/PROMO123 will work properly,
but i dont want this.
How can i archive this
www.xyz.com/Promo/PROMO123
Have you tried Routing?
Such as
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//Don't forget to add this before default one.
routes.MapRoute(
name: "PromoRoute",
url: "{controller}/{myString}",
defaults: new { controller = "Promo", action = "Index", myString = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
You need to define a route pattern for this.
If you want this to work application wide, then you will need to change the default route. But I would suggest simply adding a specific route for this controller, in addition to the default route, because you probably don't want to override the default MVC routing for the whole app or you will lose the ability to use multiple actions per controller.
See your RouteConfig, try this route (MVC pre-5):
routes.MapRoute("myRoute", "PromoRoute/{id}",
new {controller="PromoRoute", action = "Index"});
With MVC5 you can add this directly to your action assuming you've enabled attribute routes:
[Route("PromoRoute/{id}")]
public ActionResult Index(string id) {
}

MVC Routing access path

I am quite new to MVC. I am facing a problem with routing right now. My project URL is /account/Create. I can access controller and do my stuff for Create, but
I need to access /account controller because I need to write code in that level.
/account/create - I can access the code this level
/account - dont know how to access this controller
Project Stucture:
Sample Project
Controler
Model
View
What am I supposed to change in the following code?
//global.asax.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } //Parameter defaults
);
}
/account/create is accessing code in the Account controller. Create has to be a method on the Account controller (unless you modify the default routes). Any public method you define on the account controller is accessible via /account/method URL. Based on the route you posted, going to /account URL is going to call the account controller Index method:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
**new { controller = "Home", action = "Index", id = UrlParameter.Optional }** // Parameter defaults
);
That action = "Index" part above is defining what the default method on the account controller is, so going to /account URL is equivalent in this case to /account/index URL
And I just noticed that you spelled account wrong in the question, not sure if that may be your issue ;)
Update
Not sure if this is what you're after, but if you need to write code at the /Account level you can do this in the constructor of the controller.
Unless you substantially customize MVC, then controllers correspond to classes derived from Controller in mvc, and actions correspond to methods on those controllers.
What are you trying to achieve when you say you can't access the controller /Account?
The controller is only a container for Actions so you need to specify an Action. Of course, you can have a default Action in case an action isn't specified. That is specified in default the route above. It's called Index

Why the home page is not shown?

I am working on a project, and I have some controllers there.Two of them is named "Class" and "Home" controller and the Class controller has a view named "Index" and the Home one has a view named "Index".But when I run the project in local first of all my Class/Index is shown and if I want to see the home page I should write Home/Index in the address bar.But it should show Home/Index first in running.What is the problem?!
In your RouteConfig.cs(App_Start) it should be like this,
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 }
);
}
Note: Don't use the name of Class for custom codes. it is already defined by system.
Which controller is defined to run on your main root url, this all depends on your routing settings.
Please share the content of your Global.asax file to get more help on your problem.

Adding a parameter to the URL in ASP MVC

I need to have a parameter as part of my ASP MVC URL before Controller and Action:
http://www.mydomain.com/company1/Home
or
http://www.mydomain.com/company1/Clients/Detail/1
(Ideally I would like to have this as a sub-domain like this: http://company1.mydomain.com/Clients/Detail/1 so any answers solving this one is also appreciated)
I call this parameter Account. I tried adding something like this to the routing map:
"{account}/{controller}/{action}/{id}" but it gives me a 404 error when trying something like http://www.mydomain.com/company1/Home
Here is the RegisterRoutes in Global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("TestRoute", "{account}/{controller}/{action}/{id}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
Is there anything special I have to do when organising my Views folder or Controller actions?
Your error sounds like you are not giving a default for action in your route defaults.

Resources