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

时间:2021-08-26 06:55:18

Currently I am working on small application in 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:

目前我正在开发asp.net mvc中的小应用程序。它是一种某种本地化工具。我们的客户登录我们的应用程序,他们可以翻译他们使用的应用程序中显示的条款。它是这样的:

  1. login to localization site
  2. 登录本地化站点

  3. find and translate certain terms for example title for button in "Catalog" application
  4. 查找并翻译某些术语,例如“目录”应用程序中按钮的标题

  5. start that application(for example, "Catalog" application from 2.) and trough web services they update local database of terms with these that are translated
  6. 启动该应用程序(例如,来自2的“目录”应用程序)和通过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? 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. And most important, I don't have mesh desktop applications with dependency on dll's in which I have this logic.

这是我们的旧解决方案,并且工作正常。但是现在,我正在重构asp.net mvc中的翻译工具应用程序,我想,为什么我们在mvc和web服务中有单独的逻辑(加倍)?为什么我们不能只使用mvc作为Web服务...这样我只有一个逻辑(获取元素和更新),我们不需要创建wcf Web服务或其他东西。最重要的是,我没有依赖于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. What is your experience?

或者更简单的问题,我如何使用asp.net mvc作为Web服务。你有什么经历?

cheers Marko

2 个解决方案

#1


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. There are some things that web service projects provide that would be more difficult purely as a controller action. (de/serialization, wsdl, etc...)

这真的取决于你的需求。您当然可以使用ASP.NET MVC应用程序作为数据服务,但听起来您应该将共享代码梳理到公共库中并在两个应用程序中引用该库。 Web服务项目提供的一些内容纯粹作为控制器操作会更加困难。 (de / serialization,wsdl等...)

#2


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. Of course you loose type safety with the serialization process but that's pretty expected in most REST/SOAP based web services. If you really need the type safety for the consuming application, stick with WCF (or similar).

例如:jQuery很好地消耗了JsonResults,因为我可以从一个动作返回复杂的对象(带有集合,数组,嵌套对象等),并让jQuery将它反序列化回javascript对象,以便在客户端浏览器中使用。当然,您通过序列化过程可以放松类型安全性,但在大多数基于REST / SOAP的Web服务中,这是非常期待的。如果您确实需要消费应用程序的类型安全性,请坚持使用WCF(或类似的)。

I'd just create a flag to return an action as Json. I've noticed a few sites do it this way. Say you have this action:

我只是创建一个标志来返回Json的动作。我注意到有几个网站就是这样做的。说你有这个动作:

    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. 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,而不是像http://domain.com/controller/GetPeople一样调用GET请求(作为浏览器将获取视图),您只需添加ajax像这样的标志http://domain.com/controller/GetPeople?ajax=true返回Json。更合适的标志可能是'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. 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. Give it a go.

我一直在考虑将这个添加到我的mvc应用程序一段时间,但我不喜欢用这个标志修改每个动作的想法并添加更多的if语句。我的想法是创建一个自定义属性来装饰你想要这个功能的动作,属性动态地添加额外的标志,并有条件地将模型数据作为Json而不是最初指定的返回。搏一搏。

#1


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. There are some things that web service projects provide that would be more difficult purely as a controller action. (de/serialization, wsdl, etc...)

这真的取决于你的需求。您当然可以使用ASP.NET MVC应用程序作为数据服务,但听起来您应该将共享代码梳理到公共库中并在两个应用程序中引用该库。 Web服务项目提供的一些内容纯粹作为控制器操作会更加困难。 (de / serialization,wsdl等...)

#2


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. Of course you loose type safety with the serialization process but that's pretty expected in most REST/SOAP based web services. If you really need the type safety for the consuming application, stick with WCF (or similar).

例如:jQuery很好地消耗了JsonResults,因为我可以从一个动作返回复杂的对象(带有集合,数组,嵌套对象等),并让jQuery将它反序列化回javascript对象,以便在客户端浏览器中使用。当然,您通过序列化过程可以放松类型安全性,但在大多数基于REST / SOAP的Web服务中,这是非常期待的。如果您确实需要消费应用程序的类型安全性,请坚持使用WCF(或类似的)。

I'd just create a flag to return an action as Json. I've noticed a few sites do it this way. Say you have this action:

我只是创建一个标志来返回Json的动作。我注意到有几个网站就是这样做的。说你有这个动作:

    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. 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,而不是像http://domain.com/controller/GetPeople一样调用GET请求(作为浏览器将获取视图),您只需添加ajax像这样的标志http://domain.com/controller/GetPeople?ajax=true返回Json。更合适的标志可能是'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. 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. Give it a go.

我一直在考虑将这个添加到我的mvc应用程序一段时间,但我不喜欢用这个标志修改每个动作的想法并添加更多的if语句。我的想法是创建一个自定义属性来装饰你想要这个功能的动作,属性动态地添加额外的标志,并有条件地将模型数据作为Json而不是最初指定的返回。搏一搏。