简体   繁体   English

asp.net mvc作为一个宁静的服务和一个应用程序?

[英]asp.net mvc as a restful service and an application?

Currently I am working on small application in asp.net mvc. 目前,我正在asp.net mvc中的小型应用程序上工作。 It is a some kind of localization tool. 它是一种本地化工具。 Our clients login on our application and they can translate terms that shows up in our applications that they use. 我们的客户登录我们的应用程序,他们可以翻译显示在他们使用的应用程序中的术语。 It is happens like this: 它是这样的:

  1. login to localization site 登录到本地化站点
  2. find and translate certain terms for example title for button in "Catalog" application 查找并翻译某些术语,例如“目录”应用程序中按钮的标题
  3. start that application(for example, "Catalog" application from 2.) and trough web services they update local database of terms with these that are translated 启动该应用程序(例如,从2开始的“ Catalog”应用程序)并通过Web服务通过它们翻译的术语更新本地术语数据库

It is our old solution and that works fine. 这是我们的旧解决方案,效果很好。 But now, I am doing refactoring of a translating tool application in asp.net mvc and I think, why we have separate logic(doubled) in mvc and in web services? 但是现在,我正在asp.net mvc中进行翻译工具应用程序的重构,我想,为什么我们在mvc和Web服务中有单独的逻辑(加倍)? Why we can't use only mvc as a web service...this way I have only one logic(fetching elements and updating) and we don't need to create wcf web service or something. 为什么我们不能只将mvc用作Web服务...这样,我只有一种逻辑(获取元素和更新),而无需创建wcf Web服务或其他东西。 And most important, I don't have mesh desktop applications with dependency on dll's in which I have this logic. 最重要的是,我没有依赖于dll的网状桌面应用程序,而在其中具有这种逻辑。

And now the question. 现在是问题。 What I can get from controller in mvc, except of views and JsonResults...can I get collections of my objects directly? 我可以从mvc的控制器中获得什么,除了视图和JsonResults ...我可以直接获取对象的集合吗?

Or more plain question, how I can use asp.net mvc as a web services. 或更简单的问题是,如何将asp.net mvc用作Web服务。 What is your experience? 你有什么经验?

cheers Marko 让Marko欢呼

It really depends on your needs. 这真的取决于您的需求。 You can most certainly use an ASP.NET MVC application as a data service, but it sounds like you should tease out your shared code into a common library and reference that library in both applications. 您当然可以将ASP.NET MVC应用程序用作数据服务,但是听起来您应该将共享的代码整理到一个公共库中,并在两个应用程序中都引用该库。 There are some things that web service projects provide that would be more difficult purely as a controller action. Web服务项目提供的某些内容纯粹是作为控制器操作会更加困难。 (de/serialization, wsdl, etc...) (反序列化,wsdl等)

It really depends on what will consume your web service. 这实际上取决于将消耗您的Web服务的内容。

For example: jQuery consumes JsonResults well because i can return complex objects (with collections, arrays, nested objects etc) from an action and have jQuery deserialize it back to javascript object for use in the clients browser. 例如:jQuery很好地使用了JsonResults,因为我可以从一个操作返回复杂的对象(带有集合,数组,嵌套对象等),并让jQuery将其反序列化为javascript对象,以便在客户端浏览器中使用。 Of course you loose type safety with the serialization process but that's pretty expected in most REST/SOAP based web services. 当然,您会在序列化过程中松开类型安全性,但这在大多数基于REST / SOAP的Web服务中都是可以预料的。 If you really need the type safety for the consuming application, stick with WCF (or similar). 如果您确实需要使用应用程序的类型安全性,请坚持使用WCF(或类似方法)。

I'd just create a flag to return an action as Json. 我只是创建一个标志以将动作作为Json返回。 I've noticed a few sites do it this way. 我注意到一些网站采用这种方式。 Say you have this action: 假设您执行以下操作:

    public ActionResult GetPeople()
    {
        IList<Person> result = svc.GetPeople();

        return View(result);
    }

..this action's result is normally rendered into some view. ..该动作的结果通常呈现在某些视图中。 That's great, but if you want to use the action as a web service, you could simply change it to this: 很好,但是如果您要将操作用作Web服务,则可以将其更改为:

    public ActionResult GetPeople(string ajax)
    {
        IList<Person> result = svc.GetPeople();

        if (Convert.ToBoolean(ajax))
            return Json(result);
        else
            return View(result);
    }

..so if your consuming app didnt mind the serialized Json then instead of invoking the GET request like this http://domain.com/controller/GetPeople (as a browser would to get the View), you'd just add the ajax flag like so http://domain.com/controller/GetPeople?ajax=true to return the Json. ..so,如果您的消费类应用程序不介意序列化的Json,那么您只需添加ajax,而不是像这样的GET请求http://domain.com/controller/GetPeople (浏览器将获取View)即可像这样标记http://domain.com/controller/GetPeople?ajax=true以返回Json。 A more appropriate flag might be 'json' instead of 'ajax' - 'ajax' is common because this method is used to support downlevel browsers for actions that may optionally be invoked with ajax. 更合适的标志可能是'json'而不是'ajax'-常见的是'ajax',因为此方法用于支持下层浏览器执行可以选择使用ajax调用的操作。

I've been thinking of adding this to my mvc app for a while but i don't like the idea of modding every action with this flag and add more if statements. 我一直在考虑将其添加到我的mvc应用程序一段时间,但我不喜欢使用此标志修改每个动作并添加更多if语句的想法。 My idea is to create a custom attribute to decorate actions that you want this functionality for and the attribute dynamically adds the extra flag and conditionally returns the model data as Json rather than what's originally specified. 我的想法是创建一个自定义属性来装饰您需要此功能的操作,并且该属性动态添加额外的标志,并有条件地将模型数据返回为Json而不是最初指定的数据。 Give it a go. 搏一搏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM