Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
that's my method in API
C#
<pre>//   Function to Load Lvl Info
        public HttpResponseMessage getLevelInfo(int GameID,int LvlNO)
        {
            Tbl_FrequancyGames Tbl_FrequancyGames = (from u in (db.Tbl_FrequancyGames) where (u.GameID == GameID && u.LevelNo==LvlNO ) select u).FirstOrDefault();

            
                return Request.CreateResponse(HttpStatusCode.OK, Tbl_FrequancyGames);
            
        }


What I have tried:

How can i call it from MVC Controller
Posted
Updated 20-Feb-17 5:41am

1 solution

Since this method returns an HTML response you really can't call it from another MVC method. Well, unless you're going to use the HTML response but that really doesn't make any useful sense.

The answer to this is very easy. Split this method up into two methods. One exposed as the API that calls the method to get the data and formats it for the HTML response and another method that just grabs the data and returns it.
C#
public HttpResponseMessage getLevelInfo(int GameId, int LvlNO)
{
    var data = GetLevelInfoData(GameId, LvlNO);

    return Request.CreateResponse(HttpStatusCode.OK, data);
}

internal Tbl_FrequancyGames GetLevelInfoData(int GameId, int LevelNo)
{
    Tbl_FrequancyGames Tbl_FrequancyGames = (from u in (db.Tbl_FrequancyGames) where (u.GameID == GameID && u.LevelNo==LevelNo ) select u).FirstOrDefault();

    return Tbl_FrequancyGames;
}
 
Share this answer
 
Comments
Ahmed Beh Elgzar 21-Feb-17 3:15am    
that's ok but how can i call it in my controller ???
Dave Kreskowiak 21-Feb-17 8:35am    
Ummm...what? You call it just like any other method, passing in the appropriate parameters:
    var result = GetLevelInfoData(gameId, levelNo);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900