2017-01-18 8 views
0

以下はasp.netフレームワークのコードです。asp.netコアのRequestUri

protected HttpResponseMessage Created<T>(T apiResponse, ObjectId id) where T: ILinking 
    { 
     //var builder = new UriBuilder(); 

     Uri uri = new Uri(this.RequestUri(), id.ToString()); 
     apiResponse.get_Links().Add(new ApiLink(uri, "created", HttpMethod.Get)); 
     HttpResponseMessage message = base.Request.CreateResponse<T>(HttpStatusCode.Created, apiResponse); 
     message.Headers.Add("Location", uri.ToString()); 
     return message; 
    } 

私はasp.netコアで移行したいと考えています。私はそれを多くgoogleしかし、任意の解決策を見つけることができません。どうすればasp.netコアのthis.RequestUri()を宣言できますか?

ありがとうございます。

+0

私は.NETの世界では比較的新しいですが、6ヶ月前にASP.NET Core 1.0で私の冒険を始めました(以前のバージョンのリファレンスはありません)。ユーザーの実際のURIを取得しますか? この場合、次のコードが役に立ちます: 'string host = context.Request.Host.ToString();' 'string path = context.Request.Path.ToString();' – LeCintas

+1

元の 'RequestUri () 'のように見えますか?これは標準的な方法ではありません。それでも、現在のリクエストURIだけを返すと仮定すると、 'this.Request.RequestUri'を使うことができます。 – haim770

+0

" this "とは何ですか? – MindingData

答えて

0

ASP.NETコアには、「作成済み」レスポンスを簡単に返すためのいくつかの新機能が含まれています。

コントローラでは、このようなアクションを追加できます。

[HttpPost, Route("create")] 
public ActionResult CreateMyObject([FromBody]Creature newCreature) 
{ 
    string id = // some new object id 
    var newPath = new Uri(HttpContext.Request.Path, id); 
    return this.Created(newPath); 
}