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

前端 未结 6 1420
你的背包
你的背包 2021-02-09 06:42

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

相关标签:
6条回答
  • 2021-02-09 07:22

    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.

    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.

    0 讨论(0)
  • 2021-02-09 07:23

    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.

    0 讨论(0)
  • 2021-02-09 07:32

    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)

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

    0 讨论(0)
  • 2021-02-09 07:35

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

    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.

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

    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.

    0 讨论(0)
  • 2021-02-09 07:36

    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.

    0 讨论(0)
  • 2021-02-09 07:43

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

    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.

    0 讨论(0)
提交回复
热议问题