7

I have an existing Asp.Net MVC Website and I would also like to provide a Web Service from the same domain.

What is the best way to approach creating a web service in this scenerio?

Do I add to this project or...?

6
  • 5
    You could just return a JsonResult from one of your MCV Controller Actions, it might be what you need without having to include additional projects/code/dependencies? Commented Oct 18, 2010 at 11:31
  • 2
    Yep, as above. Return Json/Xml/Html from your Controllers. Serialization built into MVC framework. Of course if you want advanced security/authorization, endpoint flexibility - you might need WCF. But for basic web services, MVC is fine. Commented Oct 18, 2010 at 11:34
  • 2
    @RPM1984 - How do you implement authentication for this? Send a username and password with each request? Commented Oct 18, 2010 at 13:12
  • 1
    @Nicholas Murray - are you talking about MVC or WCF? I havent needed to authorize MVC calls before, but if you decorate the controllers with [Authorize], the Forms Auth cookie will be checked. But the problem might be that you won't be able to "redirect to login page" from the ajax call (as with regular WS's). You might need to create a wrapper for your WS calls, where you check auth. If they're authed, call desired WS, if they're not, redirect to login page. Good question though. Commented Oct 18, 2010 at 21:23
  • Also, is this an internal/external web service? Ie is this for consumption by your client-side code, or to expose over HTTP to external clients? Commented Oct 18, 2010 at 21:25

3 Answers 3

3

You should be able to add an WebService file directly to the MVC project. Right click on solution and select add new item, then select the web category and att the bottom of the list there should be Web Service.

Just remember to check that the routes does not eat up the call to the webservice.

That way the webservice can get access to the same model classes as the MVC application.

Sign up to request clarification or add additional context in comments.

Comments

3

You can add a web service to the project just as you do in regular ASP.NET web apps, however, MVC basically IS a web service. You could create a controller that handles all the requests that you want your web service to handle.

With the advent of MVC it is quite common to do applications that only ever load a view once, then use AJAX and client scripting almost the entire rest of the life of the application. Your AJAX calls just hit up action methods for their goods and then use the deliciousness that is JSON to parse the data and utilize it.

In my opinion designing a web service as a controller instead of using [WebMethods] is far simpler and a lot more fun!

Comments

1

First, the question is "what do we mean by web service?" This can mean anything from a MVC page that responds using XML, JSON or some other agreed upon format to full blown SOAP and WS-* encumbered nightmares.

Anyhow, perhaps the best place to start is the WCF restful services -- these play very nicely with MVC, including routing.

The cool kids are using openrasta.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.