【问题标题】:What's the best ASP.NET file type for a (non-SOAP, non-WSDL) web services project?(非 SOAP、非 WSDL)Web 服务项目的最佳 ASP.NET 文件类型是什么?
【发布时间】:2010-09-13 05:02:27
【问题描述】:

自 VS 2003 以来没有做过 ASP.NET 开发,所以我想节省一些时间并从别人的错误中吸取教训。

编写 Web 服务应用程序,但不是 WSDL/SOAP/等。 -- 更像是 REST + XML。

如果我想通过相同的 URI 分别处理不同的 HTTP 动词,那么在众多“新项目”选项(Web 窗体、通用处理程序、ASP.NET 处理程序等)中,哪一个最有意义。在一个完美的世界中,我希望在代码中以声明方式而不是通过 web.config 进行调度——但如果我这样让生活变得太艰难,我愿意改变。

【问题讨论】:

    标签: c# asp.net web-services


    【解决方案1】:

    这是我一直在玩的一个想法...使用风险自负,代码在我的“沙盒”文件夹中;)

    我想我想摆脱使用反射来确定运行哪个方法,使用 HttpVerb 作为键在字典中注册委托可能会更快。无论如何,此代码是没有保修的,废话,废话,废话......

    用于 REST 服务的动词

    public enum HttpVerb
    {
        GET, POST, PUT, DELETE
    }
    

    用于标记服务方法的属性

    [AttributeUsage(AttributeTargets.Method, AllowMultiple=false, Inherited=false)]
    public class RestMethodAttribute: Attribute
    {
        private HttpVerb _verb;
    
        public RestMethodAttribute(HttpVerb verb)
        {
            _verb = verb;
        }
    
        public HttpVerb Verb
        {
            get { return _verb; }
        }
    }
    

    Rest Service 的基类

    public class RestService: IHttpHandler
    {
        private readonly bool _isReusable = true;
        protected HttpContext _context;
        private IDictionary<HttpVerb, MethodInfo> _methods;
    
        public void ProcessRequest(HttpContext context)
        {
            _context = context;
    
            HttpVerb verb = (HttpVerb)Enum.Parse(typeof (HttpVerb), context.Request.HttpMethod);
    
            MethodInfo method = Methods[verb];
    
            method.Invoke(this, null);
        }
    
        private IDictionary<HttpVerb, MethodInfo> Methods
        {
            get
            {
                if(_methods == null)
                {
                     _methods = new Dictionary<HttpVerb, MethodInfo>();
                    BuildMethodsMap();
                }
                return _methods;
            }
        }
    
        private void BuildMethodsMap()
        {
            Type serviceType = this.GetType();
            MethodInfo[] methods = serviceType.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public);
    
            foreach (MethodInfo info in methods)
            {
                RestMethodAttribute[] attribs =
                    info.GetCustomAttributes(typeof(RestMethodAttribute), false) as RestMethodAttribute[];
                if(attribs == null || attribs.Length == 0)
                    continue;
    
                HttpVerb verb = attribs[0].Verb;
    
                Methods.Add(verb, info);
            }
    
        }
    
        public bool IsReusable
        {
            get { return _isReusable; }
        }
    }
    

    示例 REST 服务

    public class MyRestService: RestService
    {
        [RestMethod(HttpVerb.GET)]
        public void HelloWorld()
        {
            _context.Current.Response.Write("Hello World");
            _context.Current.Response.End();
        }
    }
    

    【讨论】:

      【解决方案2】:

      我同意 ashx,给你最大的控制权。您还可以更复杂并创建自定义 Http 处理程序。这样你就可以拦截你决定的任何扩展。当然,您可以添加一个 Http 模块并将任何请求重写为通用 ashx 处理程序。

      【讨论】:

        【解决方案3】:

        如果您不使用内置 Web 服务 (.asmx),那么您可能应该使用通用处理程序 (.ashx)。

        【讨论】:

          【解决方案4】:

          如果你需要休息,可能是 MVC。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-04-06
            • 1970-01-01
            • 2017-03-15
            • 1970-01-01
            相关资源
            最近更新 更多