一般处理程序如何区分method

叫我三三 2013-05-30 09:01:00
在MVC4中有ValuesController类,
内部有对应REST中GET,POST,PUT和DELETE方法。

但是在asp.net的中一般处理程序(ashx)如何区分ajax传来的是GET、POST、PUT还是DELETE?
谢谢~
...全文
179 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
叫我三三 2013-05-31
  • 打赏
  • 举报
回复
引用 16 楼 hdt 的回复:
奇怪,按你的图来看,貌似错误在客户端。 应该我这边在发送 put更新和delete时报的错是Method Not Allowed。 web.config我已经配置了 <system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> </system.serviceModel> <system.webServer> <modules> <remove name="WebDAVModule" /> </modules> <handlers> <remove name="WebDAV" /> </handlers> </system.webServer> 浏览器 IE10 Chrome27 都是报这个错
叫我三三 2013-05-30
  • 打赏
  • 举报
回复
上面的 string method = context.Request.QueryString["method"]; method = Request.RequestType; 改为:string method = Request.RequestType; ....
叫我三三 2013-05-30
  • 打赏
  • 举报
回复
引用 10 楼 nice_fish 的回复:
不好意思,我没说清楚,method这个是参数来的querystring定义的是get参数。
最近看了关于Rest模式get,put,post,delete含义与区别, 而且从ie5和主流浏览器全部支持这4种模式 所以想测试一下,直接使用这几个模式 以前只会用 get post
叫我三三 2013-05-30
  • 打赏
  • 举报
回复
引用 2 楼 hdt 的回复:
Request.RequestType;
测试代码

<body>
    <input type="button" onclick="test('get')" value="get" />
    <input type="button" onclick="test('post')" value="post" />
    <input type="button" onclick="test('put')" value="put" />
    <input type="button" onclick="test('delete')" value="delete" />
    <script src="../../js/jquery-1.9.1.min.js"></script>
    <script>
        var test = function (m) {
            $.ajax({
                type: m,
                dataType: "text",
                url: "../../WebHandler/testValid.ashx",
                data: [{ name: "A", value: "A1" }],
                success: function (data) {
                    alert(data);
                },
                error: function (e) {
                    alert(e.statusText);
                }
            });
        }
    </script>
</body>

HttpRequest Request = HttpContext.Current.Request;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string method = context.Request.QueryString["method"];
            method = Request.RequestType;
            string a = context.Request["A"];
            context.Response.Write(string.Format("method:{0},value:{1}",method,a));
        }
可以得到get post 得不到 put 和 delete
  • 打赏
  • 举报
回复
引用 9 楼 kansousama 的回复:
[quote=引用 6 楼 nice_fish 的回复:]
测试代码:

<body>
    <input type="button" onclick="test('get')" value="get" />
    <input type="button" onclick="test('post')" value="post" />
    <input type="button" onclick="test('put')" value="put" />
    <input type="button" onclick="test('delete')" value="delete" />
    <script src="../../js/jquery-1.9.1.min.js"></script>
    <script>
        var test = function (m) {
            $.ajax({
                type: m,
                dataType: "text",
                url: "../../WebHandler/testValid.ashx",
                data: [{ name: "A", value: "A1" }],
                success: function (data) {
                    alert(data);
                },
                error: function (e) {
                    alert(e.statusText);
                }
            });
        }
    </script>
</body>

public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string method = context.Request.QueryString["method"];
            string a = context.Request["A"];
            context.Response.Write(string.Format("method:{0},value:{1}",method,a));
        }
结果,行不通。 context.Request.QueryString["method"],接收不到method[/quote] 不好意思,我没说清楚,method这个是参数来的querystring定义的是get参数。
叫我三三 2013-05-30
  • 打赏
  • 举报
回复
引用 6 楼 nice_fish 的回复:
测试代码:

<body>
    <input type="button" onclick="test('get')" value="get" />
    <input type="button" onclick="test('post')" value="post" />
    <input type="button" onclick="test('put')" value="put" />
    <input type="button" onclick="test('delete')" value="delete" />
    <script src="../../js/jquery-1.9.1.min.js"></script>
    <script>
        var test = function (m) {
            $.ajax({
                type: m,
                dataType: "text",
                url: "../../WebHandler/testValid.ashx",
                data: [{ name: "A", value: "A1" }],
                success: function (data) {
                    alert(data);
                },
                error: function (e) {
                    alert(e.statusText);
                }
            });
        }
    </script>
</body>

public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string method = context.Request.QueryString["method"];
            string a = context.Request["A"];
            context.Response.Write(string.Format("method:{0},value:{1}",method,a));
        }
结果,行不通。 context.Request.QueryString["method"],接收不到method
chen_ya_ping 2013-05-30
  • 打赏
  • 举报
回复
其实一般都是用到get,post
leeshuiwua 2013-05-30
  • 打赏
  • 举报
回复
引用 2 楼 hdt 的回复:
Request.RequestType;
  • 打赏
  • 举报
回复
第五行代码加上:this.ChoseMethod(method, context);
  • 打赏
  • 举报
回复
就这种类似的。

   public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string method = context.Request.QueryString["method"];

        }
        //选择方法 
        private void ChoseMethod(string methond, HttpContext context)
        {
            switch (methond)
            {
                case "delete":
                    this.Hand_delete(context);
                    break;
                default:
                    break;
            }
        }
        //处理方法 
        private void Hand_delete(HttpContext context)
        {
           //your code
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
叫我三三 2013-05-30
  • 打赏
  • 举报
回复
不管method是GET、POST、PUT还是DELETE都是用context.Request[""];来接收吗?
by_封爱 2013-05-30
  • 打赏
  • 举报
回复
1#正解啊 xxoo.ashx?method=boyup 然后里面你自己判断 如果是boyup就调用boyup的方法了
真相重于对错 2013-05-30
  • 打赏
  • 举报
回复
Request.RequestType;
moonwrite 2013-05-30
  • 打赏
  • 举报
回复
我通常都是一个ashx对应一个方法的~ 也有看过传多一个参数 比如method=xxx,然后判断 再调研对应的方法
真相重于对错 2013-05-30
  • 打赏
  • 举报
回复
真相重于对错 2013-05-30
  • 打赏
  • 举报
回复
引用 11 楼 kansousama 的回复:
[quote=引用 2 楼 hdt 的回复:] Request.RequestType;
测试代码

<body>
    <input type="button" onclick="test('get')" value="get" />
    <input type="button" onclick="test('post')" value="post" />
    <input type="button" onclick="test('put')" value="put" />
    <input type="button" onclick="test('delete')" value="delete" />
    <script src="../../js/jquery-1.9.1.min.js"></script>
    <script>
        var test = function (m) {
            $.ajax({
                type: m,
                dataType: "text",
                url: "../../WebHandler/testValid.ashx",
                data: [{ name: "A", value: "A1" }],
                success: function (data) {
                    alert(data);
                },
                error: function (e) {
                    alert(e.statusText);
                }
            });
        }
    </script>
</body>

HttpRequest Request = HttpContext.Current.Request;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string method = context.Request.QueryString["method"];
            method = Request.RequestType;
            string a = context.Request["A"];
            context.Response.Write(string.Format("method:{0},value:{1}",method,a));
        }
可以得到get post 得不到 put 和 delete [/quote] 你测试过吗?
kiss筱魔 2013-05-30
  • 打赏
  • 举报
回复
加参数标识,那边获取switch选择

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧