【问题标题】:ASP.NET:New Action doesn't workASP.NET:新操作不起作用
【发布时间】:2015-08-13 04:33:13
【问题描述】:

我已经启动了一个 web api 项目,我正在尝试向我现有的控制器添加一个新操作。这是我的控制器代码和配置:

namespace QAServices.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Title = "Home Page";

            return View();
        }

        //[Route("Home/Welcome")] I have also tried this but it doesn't work.
        public HttpResponseMessage Welcome()
        {
            HttpResponseMessage response = new HttpResponseMessage();
            response.StatusCode = HttpStatusCode.OK;
            return response;
        }

        public ActionResult ProductPage()
        {
            return View();
        }
    }
}

路由配置

namespace QAServices
{
    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 }
            );
        }
    }
}

WebApiConfig

namespace QAServices
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // Configure Web API to use only bearer token authentication.
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

            // Web API routes
            config.MapHttpAttributeRoutes();

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

当我尝试运行我的新操作时,出现以下错误:

<Error>
    <Message>
        No HTTP resource was found that matches the request URI 'http://localhost:1095/Home/Welcome'.
    </Message>
    <MessageDetail>
        No type was found that matches the controller named 'Home'.
    </MessageDetail>
</Error>

我已经关注了这些,但无法找出问题所在:

【问题讨论】:

    标签: asp.net asp.net-web-api


    【解决方案1】:

    您的代码对我有用。我收到了回复StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: , Headers: { }

    您的HomeController 只是一个普通的 MVC 控制器。但是,您收到的响应似乎是 WebApi 响应。所以它似乎表明请求正在被路由到一个 ApiController。

    您的项目中是否有可能有一个 ApiController 装饰有 [RoutePrefix("Home")] 而没有 Welcome 操作方法?

    另外,如果您同时使用 ApiController 和 MVC 控制器,我会保留默认的 WebApiConfig 路由模板 api/{controller}/{id},或者至少与用于 MVC 控制器的路由模板区分开来。

    【讨论】:

      【解决方案2】:

      public HttpResponseMessage Welcome() 更改为public ActionResult Welcome()。在 ASP.NET MVC 控制器中的动作需要返回 ActionResult。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-22
        • 2023-04-01
        • 2018-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-17
        • 1970-01-01
        相关资源
        最近更新 更多