2016-07-27 96 views
7

在Scott Allen的Pluralsight課程中,「MVC框架中的控制器」模塊和「Action Results」部分中的「Asp.net Core 1.0基礎知識」中,我遇到了406 Not使用模型對象返回ObjectResult的Index操作方法的可接受錯誤。返回ObjectResult結果406不可接受

回答

11

blog post促使我做出的IMvcCoreBuilder參考,並添加JSON格式如下:

public void ConfigureServices(IServiceCollection services) 
{ 
    var mvcCore = services.AddMvcCore(); 
    mvcCore.AddJsonFormatters(); 
    services.AddSingleton(provider => Configuration); 
    services.AddSingleton<IGreeter, Greeter>(); 
} 

這讓我繼續下去。

+3

你爲什麼不只是這樣做:services.AddMvc (); – Issac

+0

你說得對。我認爲在教程中我需要爲了其他原因這樣做,但是我提出了建議的更改並且它可行。謝謝。 – Dzejms

+0

添加它作爲答案,我會接受它。:) – Dzejms

3

@json給出了正確的答案不是接受應標明:

這解決了我的問題:services.AddMvcCore()AddJsonFormatters()