类名:
public class MyJson
{
    public string getJson() {
        string json = "'name':'beijing'";
        return json;
    }
}我想在得到getJson()方法返回的字符串  然后我在浏览器中输入下面地址,显示为无法找到。请问我能否在地址栏访问一个类的方法,然后把方法返回的结果输出在页面(备注:在Asp.net的MVC框架中 可以在地址栏访问一个类的方法,比如输入http://localhost:4279/Service/getJson就能在页面上输出 'name':'beijing')http://localhost:4279/getJson

解决方案 »

  1.   

    至少POST方式是可以的
    参见
    http://dotnet.aspx.cc/file/Return-DataSet-DataTable-Using-jQuery-Ajax-WebMethod.aspx
      

  2.   

    1.在MSDN上查一下WebMethod特性,可以做到这一点.
    2.自定义一个通过反射调用的ashx,比如我是这么用的.using System;
    using System.Reflection;
    using System.Web;
    using System.Web.Services;
    using System.Web.SessionState;namespace RouteMap_Management
    {
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Handler : IHttpHandler, IRequiresSessionState
    {
    protected HttpContext page = HttpContext.Current;
    public void ProcessRequest(HttpContext context)
    {
    context.Request.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
    context.Response.ContentType = "text/plain";
    string sub = page.Request["sub"]; //子系统名称
    string fn = page.Request["fn"]; //子系统方法名称
    if (!string.IsNullOrEmpty(sub) && !string.IsNullOrEmpty(fn))
    Execute(sub, fn);
    }
    }
    //....
    /// <summary>
    /// 执行指定对象的方法(必须是以public修饰的方法),
    /// 对象必须是在RouteMap_Management程序集中,位于RouteMap_Management命名空间下,且必须从本类派生
    /// 方法不能有参数,在对象中查找该方法时,大小写敏感,
    /// 派生类中需要输入输出参数时,请调用Handler的Input/Output方法
    /// </summary>
    /// <param name="subSystemName"></param>
    /// <param name="method"></param>
    protected void Execute(string subSystemName, string method)
    {
    object obj = Assembly.Load("RouteMap_Management").CreateInstance("RouteMap_Management." + subSystemName);
    obj.GetType().InvokeMember(method, BindingFlags.InvokeMethod, null, obj, null);
    }
    }
    }
      

  3.   

    只要继承上面那个Handler类,在任意aspx.cs页面都可以写代码响应js的ajax请求.function fnGetLocation(){
    var data = {sub:'MarketChief.SetAreaHandler',fn:'GetLocation',areaid:'1'};
    //jquery封装
    $.post('../Handler.ashx', data, function(r){
    //do something
    });
    }
      

  4.   


    namespace RouteMap_Management.MarketChief
    {
    public class SetAgentHandler : Handler, IRequiresSessionState
    {
    public void GetLocation()
    {
    string areaid = base.Input("areaid");
    //do something
    }
    }
    }