创建我的MVC项目里面一个WebService [英] Creating a WebService inside my MVC project

查看:236
本文介绍了创建我的MVC项目里面一个WebService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有关于WebapiConfig.cs和RouteConfig.cs另一个线程,但我可以保证,这是一个不同的问题。

我的MVC项目是相当被我发现了,我将不得不创建一个web服务(在同一个域)在那里我会给予我的模型(无论是JSON和XML)的一个访问时开发的。

My MVC project was quite developed by the time I found out I would have to create a webservice (in the same domain) where I would grant access to one of my models (both in JSON and XML).

在为了做到这一点,我右键单击在我的控制器,并选​​择添加Web API 2控制器的操作,使用实体框架,并选择我的模型类和数据库环境。

In order to do so, I right clicked over my Controllers and selected "add Web API 2 Controller with actions, using Entity Framework" and selected also my model class and db context.

模板是相当完整的,我以为我是准备去下面的测试:

The template was quite complete and I thought I was ready to go for the following test:

namespace MVC4GMAPS.Controllers
{
    public class RestController : ApiController
    {
        private LocationDBContext db = new LocationDBContext();

        // GET api/Rest
        public IQueryable<Location> GetLocations()
        {
            return db.Locations;
        }
etc...

不幸的是,当我试图访问 https://开头本地主机:44300 / API / REST 我有一个404但是,本地主机:44300 /首页/指数保持伟大的工作。

Unfortunately, when I tried to access to https://localhost:44300/api/Rest I've got a 404. However, localhost:44300/Home/Index keeps working great.

我相信这个问题依赖于我的RouteConfig.cs,因为它期待一个{}动作和我RestController没有任何动作:

I believe the problem relies on my RouteConfig.cs, because it is expecting an {action} and my RestController doesn't have any actions:

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
etc...

我能做些什么?我可以同时创建一个WebapiConfig.cs文件?我不信。我希望你能帮帮我!

What can I do? Can I create simultaneously a WebapiConfig.cs file? I believe not. I hope you can help me!

推荐答案

你也有一个 WebApiConfig.cs ?在一个项目,该项目是从一开始的Web API我有这样的吧:

Do you also have a WebApiConfig.cs? In a project which was "Web API from the start" I have this in it:

public static class WebApiConfig
{
  public static void Register(HttpConfiguration config)
  {
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
  }
}

这,像其他CONFIGS,从名为的Global.asax.cs

This, like the other configs, is called from Global.asax.cs:

GlobalConfiguration.Configure(WebApiConfig.Register);

这应该得到的路由指向API控制器。否则,就像你说的,默认情况下从MVC定义的航线将寻找一个名为控制器上称为操作恢复 API 不存在。

That should get the routing pointed to the API controller. Otherwise, as you say, the route defined from MVC by default would be looking for an action called Rest on a controller called api which doesn't exist.

这篇关于创建我的MVC项目里面一个WebService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆