从地址栏被调用禁用的操作方法 [英] Disable action method from being called from the address bar

查看:272
本文介绍了从地址栏被调用禁用的操作方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的控制器的方法,我不希望从浏览器...

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?

推荐答案

如果您要使用这个动作只能从控制器或视图中那么你可以使用 ChildActionOnly 属性。

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

如果您要访问使用POST那么你可以使用它 [HttpPost] 属性。

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

但是,如果你想用用它 GET (即使用AJAX调用等),不希望用户使用地址栏,那么你可以按照这个来访问它< A HREF =htt​​p://helios.ca/2009/05/27/aspnet-mvc-action-filter-ajax-only-attribute/>教程,使你的行动AJAX只。

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.

或者,如果你只是想,是不是在所有的动作(即无法使用HTTP调用)的方法,那么你可以让私人或使用 [无为] 属性

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

这篇关于从地址栏被调用禁用的操作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆