Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable action method from being called from the address bar

I have a method in my controller that I don't want to be called from the address bar in the browser...

Is there a way to do that? Maybe some kind of annotation, modification in the route configuration? Which are my options?

like image 211
amp Avatar asked May 22 '13 16:05

amp


People also ask

How do you prevent a controller method from being accessed by an URL?

If you want to prevent a public controller method from being invoked, you can put the "NonAction" attribute over the method name. By default Index() action is the default action that is invoked on a controller on when no explicit action is mentioned.

How do you prevent a browser from calling an action method in MVC?

You could use the [ChildActionOnly] attribute on your action method to make sure it's not called directly, or use the ControllerContext. IsChildAction property inside your action to determine if you want to redirect.

What is action method in URL?

Action method only creates the url not the complete hyperlink, to create hyperlink we need to use Html. ActionLink covered next. To access these querystring values in the action method, we can use Request.QueryString like below. CONTROLLER ACTION METHOD public ActionResult Index() { string com = Request.


2 Answers

If you are going to use this action only from within your controller or Views then you can use ChildActionOnly attribute.

If you want to access it using POST then you can use [HttpPost] attribute.

But if you wish to use it using GET (i.e. using AJAX call etc) and don't want users to access it using address bar then you can follow this tutorial to make your actions AJAX only.

Or, if you simply want a method that is not an Action at all (i.e. cannot be called using HTTP) then you can either make it private or use [NonAction] attribute

like image 122
U.P Avatar answered Oct 07 '22 08:10

U.P


Use NonAction attribute on the method.

like image 30
Herman Kan Avatar answered Oct 07 '22 10:10

Herman Kan