C# asp.net mvc 应用程序也可以有 web 服务吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/788103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:12:20  来源:igfitidea点击:

Can an asp.net mvc application also have a web service?

c#asp.netasp.net-mvcweb-services

提问by Mike Roosa

I have an asp.net mvc application and now I need to add a web service to go along with it. What is the best solution for this? Just create a new web service project and add it to my solution then deploy it to the same web server using a different url? I would like it to be a part of the mvc application only because I have all my database code in there.

我有一个 asp.net mvc 应用程序,现在我需要添加一个 web 服务来配合它。什么是最好的解决方案?只需创建一个新的 Web 服务项目并将其添加到我的解决方案中,然后使用不同的 url 将其部署到同一个 Web 服务器?我希望它成为 mvc 应用程序的一部分,因为我所有的数据库代码都在那里。

Any suggestions would be appreciated. Thanks.

任何建议,将不胜感激。谢谢。

采纳答案by John Saunders

There's no reason not to add a web service project.

没有理由不添加 Web 服务项目。

You state that all your database code is in your MVC project. I strongly recommend you remove it from there into a separate class library project. This third project would be used both by the web service and by the MVC application.

您声明所有数据库代码都在您的 MVC 项目中。我强烈建议您将其从那里删除到一个单独的类库项目中。这第三个项目将由 Web 服务和 MVC 应用程序使用。

I also strongly recommend that you not use ASMX web services for any new development.

我还强烈建议您不要将 ASMX Web 服务用于任何新开发。

Use WCF only, unless you have no choice at all. There's a misconception that WCF services don't do SOAP - they do, and WCF has replaced ASMX.

仅使用 WCF,除非您别无选择。有一种误解,认为 WCF 服务不执行 SOAP - 他们做了,并且 WCF 已经取代了 ASMX。

回答by andleer

MVC is built on the asp.net framework. You should be able to include a web service within the same project. I haven't done it but I know that you can combine asp.net forms applications with MVC applications in the same project. The same should go for web services.

MVC 建立在 asp.net 框架之上。您应该能够在同一个项目中包含 Web 服务。我还没有做过,但我知道你可以在同一个项目中将 asp.net 表单应用程序与 MVC 应用程序结合起来。Web 服务也应该如此。

回答by BBQ Snags

Unless your application is very small I would recommend you create different projects for each logical part of the application. A good staring point is having a project for each of these:

除非您的应用程序非常小,否则我建议您为应用程序的每个逻辑部分创建不同的项目。一个很好的起点是为每个项目制定一个项目:

  • Domain objects
  • Data access
  • Web Services
  • UI (your ASP.NET MVC app)
  • 领域对象
  • 数据访问
  • 网页服务
  • UI(您的 ASP.NET MVC 应用程序)

This provides a clean separation corresponding to your architecture and supports reuse.

这提供了与您的架构相对应的干净分离并支持重用。

回答by Blake Taylor

Web service could mean a soap based web service or a RESTful web service. I can't think of any reason why you would not be able to simply add an asmx file to your project and be in business. That is the soap based route. If you want to be really cool though you can simple return xml from a controller action and implement a RESTful solution right over the MVC framework.

Web 服务可能意味着基于肥皂的 Web 服务或 RESTful Web 服务。我想不出任何原因,为什么您不能简单地将 asmx 文件添加到您的项目并开展业务。那是基于肥皂的路线。如果你想变得很酷,你可以简单地从控制器操作返回 xml 并在 MVC 框架上实现一个 RESTful 解决方案。

回答by Steven Lyons

If you want to use a regular ASP.NET asmx web service, it's certainly possible. Here's an example from Scott Hanselmanthat does just what you are asking about and it throws in some other ASP.NET technologies for good measure.

如果您想使用常规的 ASP.NET asmx Web 服务,那当然是可能的。下面是Scott Hanselman的一个示例,它满足您的要求,并引入了其他一些 ASP.NET 技术以进行很好的衡量。

All you have to do is File -> New Item -> Web Service and it should work like a regular ASP.NET application in your Mvc project.

您所要做的就是 File -> New Item -> Web Service,它应该像 Mvc 项目中的常规 ASP.NET 应用程序一样工作。

回答by Robert Sweeney

i think there's a couple of things here.

我认为这里有几件事。

you can indeed add a web service to an MVC application. you may even consider identifying the web service(s) as a script service to make REST like operations easier to perform via javascript. this may not be necessary due to your circumstances.

您确实可以向 MVC 应用程序添加 Web 服务。您甚至可以考虑将 Web 服务标识为脚本服务,以便通过 javascript 更轻松地执行类似 REST 的操作。由于您的情况,这可能不是必需的。

i think there is a stronger question as to the underlying architecture. If you are placing the web service withing your mvc application, because, your database code is already there...should it be? it might be a good time to abstract your data layer out a little. However, if you're dealing with a relatively small project and don't need the flexibilty, then certainly, add a web service right in. i guess what it really boils down to is addressing the true needs of your application.

我认为关于底层架构有一个更强烈的问题。如果您将 Web 服务与您的 mvc 应用程序一起放置,因为您的数据库代码已经存在...应该是吗?现在可能是将您的数据层抽象一点的好时机。但是,如果您正在处理一个相对较小的项目并且不需要灵活性,那么当然可以直接添加一个 Web 服务。我想它真正归结为解决您的应用程序的真正需求。