我正在使用ASP.NET MVC的新的WebAPI的网络服务,将成为了二进制文件,主要是.cab和.exe文件.
以下控制器方法似乎有效,这意味着它返回一个文件,但它将内容类型设置为application/json:
public HttpResponseMessage<Stream> Post(string version, string environment, string filetype)
{
var path = @"C:\Temp\test.exe";
var stream = new FileStream(path, FileMode.Open);
return new HttpResponseMessage<Stream>(stream, new MediaTypeHeaderValue("application/octet-stream"));
}
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?
我一直在使用WebApi进行开发,并已转移到WebApi2,其中Microsoft引入了一个IHttpActionResult似乎建议用于返回a 的新接口HttpResponseMessage.我对这个新接口的优点感到困惑.这似乎主要是公正提供SLIGHTLY更简单的方法来创建一个HttpResponseMessage.
我认为这是"为抽象而抽象"的论点.我错过了什么吗?除了节省一行代码之外,使用这个新接口可以获得什么样的真实优势?
旧方式(WebApi):
public HttpResponseMessage Delete(int id)
{
var status = _Repository.DeleteCustomer(id);
if (status)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
else
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
}
Run Code Online (Sandbox Code Playgroud)
新方式(WebApi2):
public IHttpActionResult Delete(int id)
{
var status = _Repository.DeleteCustomer(id);
if (status)
{
//return new HttpResponseMessage(HttpStatusCode.OK);
return Ok();
}
else
{
//throw new HttpResponseException(HttpStatusCode.NotFound);
return NotFound();
}
}
Run Code Online (Sandbox Code Playgroud) 我想编写一个新的REST样式API并查看ServiceStack并且非常喜欢它.但是,我已经看到Microsoft已经发布了ASP.Net Web API项目,作为新MVC 4 beta的一部分.有没有人看过新的Web API项目?你能给出每个系统的优缺点吗?
ASP.NET MVC4 Web API应用程序定义了保存客户的post方法.客户在POST请求正文中以json格式传递.post方法中的customer参数包含属性的空值.
如何解决这个问题,以便发布的数据作为客户对象传递?
如果可能的话Content-Type:application/x-www-form-urlencoded应该使用,因为我不知道如何在发布表单的javascript方法中更改它.
控制器:
public class CustomersController : ApiController {
public object Post([FromBody] Customer customer)
{
return Request.CreateResponse(HttpStatusCode.OK,
new
{
customer = customer
});
}
}
}
public class Customer
{
public string company_name { get; set; }
public string contact_name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
请求:
POST http://localhost:52216/api/customers HTTP/1.1
Accept: application/json, text/javascript, */*; q=0.01
X-Requested-With: XMLHttpRequest
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
{"contact_name":"sdfsd","company_name":"ssssd"}
Run Code Online (Sandbox Code Playgroud) 我意识到会话和REST并不完全齐头并进,但是使用新的Web API无法访问会话状态吗?HttpContext.Current.Session永远是空的.
我正在尝试从Web API发布一个JsonObject使用HttpClient.我不太确定如何解决这个问题,并且在示例代码方面找不到多少内容.
这是我到目前为止所拥有的:
var myObject = (dynamic)new JsonObject();
myObject.Data = "some data";
myObject.Data2 = "some more data";
HttpClient httpClient = new HttpClient("myurl");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = httpClient.Post("", ???);
Run Code Online (Sandbox Code Playgroud)
我想我需要把我JsonObject当成一个StreamContent但是我已经挂了这一步.
我在Web上看到了大量使用新HttpClient对象(作为新Web API的一部分)的示例,应该有HttpContent.ReadAsAsync<T>方法.但是,MSDN没有提到这种方法,IntelliSense也没有找到它.
它去了哪里,我该如何解决它?
我正在开发一个应该响应的网站,以便人们可以通过手机访问它.该网站有一些安全的部分,可以使用谷歌,Facebook,...等登录(OAuth).
服务器后端是使用ASP.Net Web API 2开发的,前端主要是带有Razor的AngularJS.
对于身份验证部分,一切都在包括Android在内的所有浏览器中正常运行,但Google身份验证在iPhone上无效,它给了我这个错误消息
Refused to display 'https://accounts.google.com/o/openid2/auth
?openid.ns=http://specs.openid.ne…tp://axschema.org/namePerson
/last&openid.ax.required=email,name,first,last'
in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.
Run Code Online (Sandbox Code Playgroud)
现在到目前为止我担心我的HTML文件中不使用任何iframe.
我用Google搜索,但没有回答让我解决问题.
我正在尝试在我的Web API应用程序中支持JWT承载令牌(JSON Web令牌),我迷路了.
我看到对.NET Core和OWIN应用程序的支持.
我目前正在IIS中托管我的应用程序.
如何在我的应用程序中实现此身份验证模块?有什么办法可以使用<authentication>类似于我使用表单/ Windows身份验证的方式配置吗?
由于Microsoft Web API不是MVC,因此您无法执行以下操作:
var a = Request.MapPath("~");
Run Code Online (Sandbox Code Playgroud)
也不是这个
var b = Server.MapPath("~");
Run Code Online (Sandbox Code Playgroud)
因为它们在System.Web命名空间下,而不是System.Web.Http命名空间.
那么如何计算Web API中的相对服务器路径?
我曾经在MVC中做过类似的事情:
var myFile = Request.MapPath("~/Content/pics/" + filename);
Run Code Online (Sandbox Code Playgroud)
哪个会给我磁盘上的绝对路径:
"C:\inetpub\wwwroot\myWebFolder\Content\pics\mypic.jpg"
Run Code Online (Sandbox Code Playgroud) asp.net-web-api ×10
c# ×4
asp.net ×3
asp.net-mvc ×2
.net ×1
angularjs ×1
google-oauth ×1
httpcontent ×1
httpresponse ×1
javascript ×1
json ×1
jwt ×1
security ×1
servicestack ×1
wcf ×1
wcf-web-api ×1