Reputation: 8578
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?
Upvotes: 2
Views: 120
Reputation: 1046
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.
Upvotes: 0
Reputation: 446
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
Upvotes: 2
Reputation: 7525
Add ChildActionOnly
attribute to the controller method so MVC will not let user call it directly via http://localhost:port/Home/About
Upvotes: 3