私は1つのクラス "student"を作成しました。リストに3つの要素が追加されました。私は1つの名前を取得したい。コンパイル時にエラーが発生します。私はwebapiから名前を取得したい。そのスローエラーが表示されます。暗黙的にタイプモデルをihttpactionresultに変換できません
プットを出したいroutes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
をルーティング
using testWEBAPI.Models;
namespace testWEBAPI.Controllers
{
public class MyapiController : ApiController
{
public List<student> s = new List<student>()
{
new student{name="krishna",city="hyderbad"},
new student{name="priya",city="mumbai"},
new student{name="sandeep",city="jamshedpur"}
};
public IHttpActionResult getnames()
{
return Ok(s.ElementAt(0).name);
}
}
}
- クリシュナ、ハイデラバード
's.ElementAt(0)' 'student'を返します使用しますオブジェクトではなく、 'IHttpActionResult' – Pikoh
私はどうすればいいのですか、Web APIの文字列を取得する必要がありますか? – sanjay
WebApi 2を使用している場合は、返すタイプを示すために 'ResponseType'属性を使います。 WebApi 2を使用していない場合は、私が強くお勧めします。ルーティングははじめての方がはるかに簡単で簡単です。 –