Question

Let's say I have a default project in MVC. I change the About link to be an Ajax form so that it only loads that part of the page. And of course I change the method to return PartialView('About') instead of View('About'). However, the problem arises when a user types in and goes to a link called localhost:port/Home/About. Now it loads about part but without the layout (without css, js, menu bars, etc.)! How can I prevent him from going into such page? Or maybe display an error page instead? Or even redirect him to go to View('About')? Options are endless, but how to know that the call was in a "wrong" way?

Was it helpful?

Solution

Add ChildActionOnly attribute to the controller method so MVC will not let user call it directly via http://localhost:port/Home/About

OTHER TIPS

You can check in your action method if a request is ajax or not with

Request.IsAjaxRequest()

and take whatever action you need based on this.

This checks if the X-Requested-With header has been set to XMLHttpRequest by the calling client.

Detecting IsAjaxRequest() with ASP.NET MVC and JQuery Form Plugin / File Upload

as @Zabavsky it is seems like a duplicate...

another (very simple) solution may be to just seprate the methods. you anyway have seperate views. so the ajax call will trigger AboutAjax() and render the partialView and About() stays as is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top