2
votes

I have created a web api in mvc 4. my controller is-

 [HttpPost]
    public string signIn(string socialId, string email)
    {
        try
        {
            using (TourismModelContainer db = new TourismModelContainer())
            {
                var user = (from log in db.UserDetails
                            where ((log.FacebookId == socialId || log.GoogleId == socialId) && log.IsActive==true)
                            select new { log.Id, log.Mobile, log.Gender, log.Country }).FirstOrDefault();
                if (user != null)
                {
                    string json = new Utills().ObjectToJson(user); ;
                    if (email == null || email == "")
                    {
                        return json;
                    }
                    else
                    {
                        bool isEmail = db.UserDetails.Where(o=>o.IsActive==true).Any(obj => obj.Email == email);
                        if (isEmail)
                            return "1";
                        else
                            return json;
                    }
                }
                else
                {
                    return "-1";
                }
            }
        }
        catch (Exception ex)
        {
            return "";
        }
    }

my WebApiConfig.cs file is

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

and my route config is -

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

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

but when i call this sign in service, I get following error-

{ "Message": "No HTTP resource was found that matches the request URI 'http://localhost:58851/api/UpTourism/signIn'.", "MessageDetail": "No action was found on the controller 'UpTourism' that matches the request." }

Please help me regarding this.

1
Make sure that the name of your controller is (UpTourismController)Ala
Yes Its UpTourismController.Vivek Mishra
Please post code of Controller class itself (with single empty action, there is no reason to show wall of code not related to routing), actual calls to RegisterRoutes/Register methods and how you call the method (whatever you use to construct post request).Alexei Levenkov
I am using postman to make a post request. and my routes and webapi config is mentioned above.Vivek Mishra
Not good that your controllers and API routes goes to same path *api*/{controller}/{action}/{id}Aleksej Vasinov

1 Answers

2
votes

Try this:

During HttpPost you can send only one parameter to action.So clubbing the two parameters you have inside a class and sending that object is much easier and convenient.I created a Demo class.And my controller is CustomController.

enter image description here

enter image description here

enter image description here