0

I'm making a simple website in Asp.net MVC 4. I'm using default Identity models. There are many partial views that I am using.

The problem is that if someone passes the path to a partial view, the view opens up but all messed up (because it's a partial view). But I want to restrict anyone from opening a partial view except from some within some view and not by simply putting the link in the address bar and hitting enter..

For example the localhost:1234/abc/MainPage view may open the MainPage and this MainPage may open up a partial view (PView for example) but simply by putting localhost:1234/abc/PView should not open any view and should redirect to its parent view eg MainPage. Is there any method to do that? Similar to what we do for checking roles and authorization via Identiy model i.e. [Authorize] tag

2 Answers 2

2

You can mark your action as a child action

[ChildActionOnly]
public ActionResult ActionForMyPartial()
{
    ...
}

If it is handled by AJAX requests you can check that it matches the request

public ActionResult ActionForMyPartial()
{
    if (Request.IsAjaxRequest())
    {
        return PartialView();           
    }
    return View();
}
Sign up to request clarification or add additional context in comments.

Comments

0

I don't understand at all but i think you are looking something like this: [Authorize(Roles = "Administrator")] you can use that adding above your method that is rendering the partial view like this [Authorize(Roles = "Administrator")] [HttpGet] public IActionResult renderMyPartial(string myparam) { //some code }

i hope this help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.