Tadej Vengust
Tadej Vengust

Reputation: 1401

View from custom controller overriding default view

I'm using orchardcms 1.9 (no tag created jet). I am writing a custom module that implements its own controller that calles a service wich check some information and based on the service response I either redirect or let the user stay on the page.

The module is on the default layer in other words it is on everypage. So when user tries to log in or register this module checks information normally.

This is my route:

 new RouteDescriptor {
                    Priority = -1,
                    Route = new Route(
                        "{*path}", // this is the name of the page url
                        new RouteValueDictionary {
                            {"area", "modulename"}, // this is the name of your module
                            {"controller", "controllername"},
                            {"action", "Redirect"}
                        },
                        new RouteValueDictionary(),
                        new RouteValueDictionary {
                            {"area", "modulename"} // this is the name of your module
                        },
                        new MvcRouteHandler())

and this is my controller:

 public ActionResult Redirect()
        {
            String response = _authService.VerifyRegistration(_orchardServices.WorkContext.CurrentUser);

        if (response.Equals("2"))
        {
            Response.Redirect("~/Registration");
        }
        else if (response.Equals("3"))
        {
            Response.Redirect("~/Users/Account/LogOn");
        }


        return View();
    }

What happens is that when I go to registration or login controller triggers, checks the infromation, says no redirect needed then returns view. But because the view is empty my page is blank instead of its default login/registration form.

How can I solve this? Am I making a mistake in routing that I somehow override the default view (I tried different priorities but same response).

Upvotes: 0

Views: 69

Answers (1)

Paul Devenney
Paul Devenney

Reputation: 1319

Your route overrides all routes ({*path}. So when you redirect, you redirect to....your redirector I guess. Therefore the view you are rendering is the one for your controller, not the page you were after.

Whatever the logic flaw - this is not a good way to globally control authorization type scenarios on your site. If you meant to have a single page that people might go to (e.g. http://www.mysite/welcome) then your problem is that your route is too global. However, if, as your code suggests that you want to create a "all pages" check to see if you should go to login or register, then you should implement an authorization filter. An example of an authorization filter (for a slightly different purpose) can be found at https://stackoverflow.com/a/30377097/1638254 . You are looking to fill in the OnAuthorization method with suitable code to redirect the user (or let them through

Upvotes: 1

Related Questions