balexandre
balexandre

Reputation: 75103

How to create a simple landing page in MVC2

I'm trying to create a http://domain.com/NotAuthorized page.

went to Views\Shared and added a View called NotAuthorized witch originates the file name NotAuthorized.aspx

alt text

in my Routes I wrote

routes.MapRoute(
    "NotAuthorized", // Route name
    "NotAuthorized.aspx"  // Route Url
);

but every time I access http://domain.com/NotAuthorized I get an error

The resource cannot be found.

What am I missing?

How can access this without using View("NotAuthorized") in the Controller, in other words, not passing through any controller.

Upvotes: 0

Views: 616

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039060

You can't access views directly without passing through a controller. All pages in the Views folder cannot be served directly. So one way to accomplish what you are looking for is to write a custom[Authorize] attribute and set the error page:

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
    if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
    {
        base.HandleUnauthorizedRequest(filterContext);
    }
    else
    {
        filterContext.Result = new ViewResult { ViewName = "NotAuthorized" };
    }
}

Upvotes: 1

balexandre
balexandre

Reputation: 75103

I still have no idea on how to accomplish it, but what I did was use the Home Controller and create an Action called NotAuthorized

public ActionResult NotAuthorized()
{
    return View();
}

And add a route like

routes.MapRoute(
    "NotAuthorized", // Route name
    "NotAuthorized", // URL with parameters
    new { controller = "Home", action = "NotAuthorized" } // Parameter defaults
);

And works fine now, I can easily redirect in any part of my Business Logic to /Notauthorized and that will route fine.

Upvotes: 0

Related Questions