c# - MVC 4路由找不到 Controller

标签 c# asp.net-mvc asp.net-mvc-4

我在我的新 MVC 5 应用程序中找不到 Controller 路由。我一直在关注一些教程,一切似乎都写得正确,但在浏览器中浏览地址时,我总是得到:

No HTTP resource was found that matches the request URI "//localhost:50473/api/tile"

/Controller/ProductController.cs

using System;
using System.IO;
using System.Web;
using System.Net;
using coal.Models;
using System.Linq;
using System.Text;
using System.Net.Http;
using System.Web.Http;
using System.Collections.Generic;

namespace coal.Controllers
{
    public class ProductController : ApiController
    {
        public void GetAllProducts() { }
        [HttpGet]
        public string Tiles(string urls) { }
    }
}

/App_Start/WebApiConfig.cs

namespace coal
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

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

我知道我遗漏了一些东西,只是无法弄清楚它是什么。任何见解都会有所帮助。

谢谢

最佳答案

您的方法名称是 Tiles,但您正试图点击 tile

public string Tiles(string urls) { }

编辑:更新为在正确的使用路径中包含 ProductController:

此外,您的路由定义为 api/{controller}/{action},您的 Controller 名称为 ProductController。

您应该使用的 url 是:api/product/tiles,除非您创建一个将 Product/tiles 操作映射到 api/tiles 的路由 路线。

你的路线应该是这样的:

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

关于c# - MVC 4路由找不到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23346798/

相关文章:

ajax - 在我的 asp.net mvc 5 Web 应用程序中处理 http 错误的推荐方法

c# - 如何在 C# 的 LabVIEW 中设置集群中变量的值?

c# - MySql、Ninject 和 NHibernate - 已经有一个与此连接关联的打开的 DataReader,必须先将其关闭

asp.net-mvc - Moq - 如何模拟 HttpContext.Request.Url.Authority 对象等非虚拟成员?

asp.net-mvc - 如何将 asp.net mvc 部署到我的桌面,以便网络中的所有计算机都可以看到它?

asp.net - 如何在 Asp.net MVC 4 应用程序中使用 SimpleMemberShip Provider

asp.net-mvc - 使用共享库跨项目共享 Razor View

javascript - HttpModule Filter_OnTransformString 导致部分静态文件失效(即css、js

c# - 控制更改其他 UserControl 上的控制

c# - SendTwoFactorCodeAsync 在成功登录后返回 'UserId not found',使用具有依赖性的 SignManager