从浏览器调用asmx Web服务

编程入门 行业动态 更新时间:2024-10-27 00:27:24
本文介绍了从浏览器调用asmx Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好b $ b 我正在使用 framework 4.0开发ASP.NET Web Forms应用程序 我想在我的应用程序中使用Web服务 - >可以从浏览器访问(通过输入URL) - >返回JSON 在我的网络配置文件中,我已经添加了

< webServices > < 协议 > < add 名称 = HttpGet / > < ; / protocols > < / webServices >

这是我的asmx服务的样子

[WebMethod] [ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)] public List< Department> BindDepartments(int faculty = 0,string university =,string branch =) { List< Department> departmentList = new List< Department>(); DepartmentBL departmentBL = new DepartmentBL(); try { JavaScriptSerializer js = new JavaScriptSerializer(); Context.Response.Clear(); Context.Response.ContentType =application / text; Context.Response.Write(js.Serialize(departmentBL.getDepartmentsByFaculty(faculty,university,branch))); } catch(例外情况) { } 最后 { departmentBL = null; } return departmentList; }

使用此功能,当我从浏览器调用我的服务时,它返回XML和JSON的混合

[{DepartmentId:2,DepartmentName:Computer Science,DepartmentShortName: CS, FacultyId:3, FacultyName: 科学, 状态:假的, CreatedBy:0, CreatedTime: \ /日期(1406194645653)\ /, ModifiedBy: 0,ModifiedTime:\ / Date(-62135596800000)\ /}]<?xml version =1.0encoding =utf-8?> < ArrayOfDepartment xmlns:xsi =www.w3/2001/XMLSchema-instancexmlns:xsd =www.w3/2001/XMLSchemaxmlns = tempuri//>

如果我没有序列化并只返回我的C#List对象,那么我将获得纯XML数据。 请在这里帮助我。我做错了什么?

解决方案

你将json结果写回响应并再次返回列表集合。 如何尝试这样做?

[WebMethod] [ScriptMethod(UseHttpGet = true ,ResponseFormat = ResponseFormat.Json) ] public string BindDepartments( int faculty = 0 , string university = , string branch = ) { List< department> departmentList = new List< department>(); DepartmentBL departmentBL = new DepartmentBL(); 尝试 { JavaScriptSerializer js = new JavaScriptSerializer(); Context.Response.Clear(); Context.Response.ContentType = application / json; // json response // Context.Response.Write(js.Serialize(departmentBL.getDepartmentsByFaculty(faculty,university,branch))); return js.Serialize(departmentBL.getDepartmentsByFaculty(faculty,university,branch)); } catch (例外情况) { } 最后 { departmentBL = null ; } // xml回复 // return departmentList; } < / department > < / 部门 >

Hi I am working on a ASP.NET Web Forms application using framework 4.0 I want to use web services in my application which will -> be accessible from a browser (by typing in the URL) -> return JSON In my web config file, I have already added

<webServices> <protocols> <add name="HttpGet"/> </protocols> </webServices>

Here is what my asmx service looks like

[WebMethod] [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] public List<Department> BindDepartments(int faculty = 0, string university = "", string branch = "") { List<Department> departmentList = new List<Department>(); DepartmentBL departmentBL = new DepartmentBL(); try { JavaScriptSerializer js = new JavaScriptSerializer(); Context.Response.Clear(); Context.Response.ContentType = "application/text"; Context.Response.Write(js.Serialize(departmentBL.getDepartmentsByFaculty(faculty, university, branch))); } catch (Exception ex) { } finally { departmentBL = null; } return departmentList; }

Using this, when I call my service from the browser, it returns a mix of XML and JSON

[{"DepartmentId":2,"DepartmentName":"Computer Science","DepartmentShortName":"CS","FacultyId":3,"FacultyName":"Science","Status":false,"CreatedBy":0,"CreatedTime":"\/Date(1406194645653)\/","ModifiedBy":0,"ModifiedTime":"\/Date(-62135596800000)\/"}]<?xml version="1.0" encoding="utf-8"?> <ArrayOfDepartment xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:xsd="www.w3/2001/XMLSchema" xmlns="tempuri/" />

If I do not serialize and just return my C# List object, then I get pure XML data. Please help me out here. What am I doing wrong?

解决方案

You write json result to response and return list collection again. How about trying like this?

[WebMethod] [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] public string BindDepartments(int faculty = 0, string university = "", string branch = "") { List<department> departmentList = new List<department>(); DepartmentBL departmentBL = new DepartmentBL(); try { JavaScriptSerializer js = new JavaScriptSerializer(); Context.Response.Clear(); Context.Response.ContentType = "application/json"; //json response // Context.Response.Write(js.Serialize(departmentBL.getDepartmentsByFaculty(faculty, university, branch))); return js.Serialize(departmentBL.getDepartmentsByFaculty(faculty, university, branch)); } catch (Exception ex) { } finally { departmentBL = null; } //xml response //return departmentList; } </department></department>

更多推荐

从浏览器调用asmx Web服务

本文发布于:2023-10-07 10:48:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469187.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:浏览器   asmx   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!