Srusti Thakkar
Srusti Thakkar

Reputation: 1901

Method not found even it exist in controller

Sample RequestI am using MVC Web API. I am facing one weird issue from past day. I have declared multiple method in constructor and try to called it. But every time it returns line "Method Not Found Exception". But I have declared method in controller and added in config also. My code for controller is :

public class GlobalController : BaseApiController
{
    public GlobalController()
    {

    }

    [HttpGet]
    [ActionName("Test")]
    [AllowAnonymous]
    public IHttpActionResult Test(string id = "")
    {
        return Ok(id);
    }


    [HttpPost]
    [ActionName("Login")]
    [ResponseType(typeof(CacheUser))]
    [AllowAnonymous]
    [Route("api/Global/Login")]
    public IHttpActionResult Login(LoginModel model)
    {
        try
        {
            CacheUser loginUser = CacheLib.Login(model);

            return Ok(loginUser);
        }
        catch (Exception ex)
        {
            throw StaticLib.InternalServerError(ex);
        }
    }
}

I am able to call first method(i.e. Test). But whenever I try to call Login it returns method not found.

WebApiConfig contains :

// Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "Global_DefaultApi",
            routeTemplate: "api/Global/{action}",
            defaults: new { controller = "Global", action = "Test" }
        );

Any idea?

Stack trace is :

{"Message":"An error has occurred.","ExceptionMessage":"Method not found: 'System.Web.Http.HttpResponseException HSSystem.Libraries.API.StaticLib.InternalServerError(System.Exception)'.","ExceptionType":"System.MissingMethodException","StackTrace":"

Upvotes: 0

Views: 771

Answers (1)

CodeCaster
CodeCaster

Reputation: 151674

The signature of the missing method is, according to the exception:

System.Web.Http.HttpResponseException HSSystem.Libraries.API.StaticLib.InternalServerError(System.Exception)

This means that your controller compiles just fine against your HSSystem.Libraries.API.StaticLib, but that at runtime another assembly is loaded. So your GlobalController.Login() method is routed to and called, but when JITting its code (and finding the methods of other classes that are called within there), the exception is thrown because this specific version of the method StaticLib.InternalServerError() can't be found. Perhaps you changed the return type of that method, or added an optional parameter. This breaks the ABI.

Loading the wrong assembly can have many causes. Perhaps you reference another project which has a reference to an older or newer version of that assembly, which doesn't have that exact method, and that version gets copied on build. Or you have some kind of plugin system. Or, or, or.

Make sure you compile against the same version that's deployed at runtime.

Upvotes: 1

Related Questions