【问题标题】:MVC5 WebApi2 route not workingMVC5 WebApi2 路由不起作用
【发布时间】:2017-09-29 17:47:32
【问题描述】:

我检查了与 mvc5、webapi2 相关的链接,但无法找出我的错误。

我的问题: /api/EBanking/CheckLogin 不执行 ebankingcontroller 中 checkLogin 方法的代码

已检查链接:

Custom Routing not working in MVC5

WebAPI2 and MVC5 route config

QueryString with MVC 5 AttributeRouting in Web API 2

App_start 代码:

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AutoMapperCentralAppConfig.Configure();
    }

RouteConfig.cs

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

WebApiConfig.cs

public static string UrlPrefixRelative { get { return "~/api"; } }
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

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

API 控制器:

    [RoutePrefix("api/EBanking")]
public class EBankingController : ApiController
{
    public EBankingController()
    {
    //some other code, it runs
    }

    [HttpGet, HttpPost]
    [Route("CheckLogin")]
    public IEnumerable<usr06user_role> CheckLogin(string UserName, string Password)
    {
    //main code which doesn;t runs
    }

    public IEnumerable<usr06user_role> GetAll()
    {
    //test code which runs when we call: /api/ebanking/
    }

结果截图:

【问题讨论】:

    标签: c# asp.net-mvc-5 routes asp.net-web-api2


    【解决方案1】:

    在您的 WebApiConfig.cs 中为基于操作的路由添加另一个路由,如下所示:

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

    这个"api/{controller}/{action}/{id}" 将允许调用 api/ebanking/checklogin

    您甚至可以在属性中添加完整的路线,例如:

    [Route("api/EBanking/CheckLogin")]
    public IEnumerable<usr06user_role> CheckLogin(string UserName, string Password)
    {
        //main code which doesn;t runs
    }
    

    【讨论】:

    • 尝试了两种建议的方法,添加了额外的路线图但仍然无法正常工作...添加我的结果屏幕截图
    • 哪些路线有效? api/ebanking 有效吗?您还有其他可行的路线吗?
    • 我有一个建议。尝试一步一步来..首先摆脱控制器级别的路由前缀/路由。添加具有不同路由前缀的新 httpget。你的第一个目标应该是让 2 HttpGet 在 web api 控制器中工作,上面的代码应该可以帮助你。默认情况下,Web Api 控制器让您拥有一个 httpget、一个 httppost 等等。一旦你可以得到一个简单的工作,你应该能够继续使用参数进行 CheckLogin。
    • 我发现查询字符串中缺少参数名称用户名和密码字段会导致问题...
    猜你喜欢
    • 2014-06-14
    • 2014-12-27
    • 2016-01-06
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    相关资源
    最近更新 更多