Tag: asp.net web api

如何从C#方法返回Json对象

我正在尝试修复需要Json响应的ASP.NET WebAPI方法。 但是它会返回一个字符串。 最初它是在撤回XML格式,但我已将此行添加到App_Start \ WebApiConfig.cs中的mvc代码中,以便默认返回Json。 config.Formatters.Remove(config.Formatters.XmlFormatter); 我们使用NewtonSoft更新了c#方法如下: public string Get() { string userid = UrlUtil.getParam(this, “userid”, “”); string pwd = UrlUtil.getParam(this, “pwd”, “”); string resp = DynAggrClientAPI.openSession(userid, pwd); JsonSerializer ser = new JsonSerializer(); string jsonresp = JsonConvert.SerializeObject(resp); return resp; } resp var以字符串类型的forms返回: “{status:\”SUCCESS\”,data:[\”4eb97d2c6729df98206cf214874ac1757649839fe4e24c51d21d\”]}” 和jsonresp var看起来像这样: “\”{status:\\\”SUCCESS\\\”,data:[\\\”4eb97d2c6729df98206cf214874ac1757649839fe4e24c51d21d\\\”]}\”” 在Chrome的F12开发工具中,数据对象是: “”{status:\”SUCCESS\”,data:[\”4eb97d2c6729df98206cf214874ac1757649839fe4e24c51d21d\”]}”” 在Console工具中,angular.fromJson(data)的结果: “{status:”SUCCESS”,data:[“4eb97d2c6729df98206cf214874ac1757649839fe4e24c51d21d”]}” 我将不胜感激如何正确返回Json对象,而不是任何字符串类型。 谢谢。 短发 ————更新—————- […]

跨两个Web API项目共享OAuth令牌

我创建了一个带有OAuth令牌认证的Web API应用程序。 当令牌服务器在与服务相同的应用程序上运行时,这没有问题。 但是,我想将授权服务移动到自己的应用程序(VS项目)中,并将其用于我正在处理的几个Web API项目中。 但是,当我将授权逻辑隔离到它自己的项目中时,原始服务不再将生成的令牌视为有效。 我的问题是,一个Web API项目是否有可能为另一个Web API项目生成令牌以进行validation? 这是我的授权服务和原始服务的OWIN启动代码 认证服务: public void Configuration(IAppBuilder app) { // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888 HttpConfiguration config = new HttpConfiguration(); ConfigureOAuth(app); WebApiConfig.Register(config); app.UseWebApi(config); app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); } private void ConfigureOAuth(IAppBuilder app) { OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions() { AllowInsecureHttp = true, TokenEndpointPath = new […]

在Azure Web App上运行Selenium

我有一个Azure Web应用程序,当我在控制器上调用Action时,我想用它来屏幕抓取网站,就像这样。 var driver = new PhantomJSDriver(); driver.Url = “http://url.com”; driver.Navigate(); var source = driver.PageSource; var pathElement = driver.FindElementByXPath(“//table[@class=’someclassname’]”); string innerHtml = “”; IJavaScriptExecutor js = driver as IJavaScriptExecutor; if (js != null) { innerHtml = (string)js.ExecuteScript(“return arguments[0].innerHTML;”, pathElement); } return innerHtml; 这在本地工作正常,但是当我上传到我的Azure Web App时,我收到此错误 无法在http:// localhost:51169 /上启动驱动程序服务 我认为这与防火墙有关,因为我需要在应用程序第一次运行时在防火墙设置中批准PhantomJS。 我的问题是如何在Azure中部署这个工作? 它是否可能,或者我是否需要将其配置为unit testing并在Visual Studio中运行?

OverrideAuthenticationAttribute的用途是什么?

我在当前的Web API项目中遇到了一个标有System.Web.Http.OverrideAuthenticationAttribute的控制器方法,我很好奇这是为了什么? 在Google和Stackoverflow中搜索无法回答这个问题。 MSDN文档不包含太多信息。 它只说以下内容: 表示filter属性,该属性覆盖在更高级别定义的身份validationfilter。 另外,我已经看了一下来源: public sealed class OverrideAuthenticationAttribute : Attribute, IOverrideFilter, IFilter { public bool AllowMultiple { get { return false; } } public Type FiltersToOverride { get { return typeof(IAuthenticationFilter); } } } 但这并没有带来太多启示。 那么有人可以解释使用OverrideAuthenticationAttribute的目的是什么? 请提供一些用例以便更好地理解。

Web API和MVCexception处理

我们目前正在将我们的Web表单系统重新开发为Web API和MVC(这对我们来说是新技术)到目前为止,一切似乎都没问题,但我们正在努力将Web API应用程序中的错误发送回MVC应用程序。 我们意识到我们需要捕获任何exception,并将这些exception转换为HTTP响应 Web API Product控制器如下所示: public HttpResponseMessage GetProducts() { BAProduct c = new BAProduct(); var d = c.GetProducts(); if (d == null) return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, “This is a custom error message”); else return Request.CreateResponse(HttpStatusCode.OK, d); } MVC应用程序将通过以下代码调用Web API: – public T Get() using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri(Config.API_BaseSite); client.DefaultRequestHeaders.Accept.Add(new […]

使用WebApi进行OData分页($ inlinecount)

我正在使用OData分页从web api调用返回的一长串项目。 我可以通过url使用开始和结束索引过滤数据。 我的问题是,我怎么知道物品的总数? 所以我可以在我的移动设备上显示第1页(共3项)(20项),该设备调用web api。

Windows窗体的MVC WebAPI身份validation

我正在尝试制作一个Windows窗体应用程序,它插入ASP.NET MVC WebAPI公开的某些服务,但是在validation/登录部分遇到了很多麻烦。 我似乎无法找到一个示例,只是演示如何从Windows窗体执行此操作,我发现的一切似乎非常复杂,包括很多非常深的管道,或似乎针对其他ASP.NET网站,而不是Windows窗体。 有什么我想念的吗? 这是不可能的吗? 或者它只是不打算? 我已经看过像.NET WebApi身份validation那样声称要做的事情,但是我没有看到如何使用Windows窗体的角度来构建 cookie。 我也经历了http://blogs.msdn.com/b/webdev/archive/2012/08/26/asp-net-web-api-and-httpclient-samples.aspx并且仍然运气不佳。

Web API路由 – 找到了与请求匹配的多个操作

我有这条路线: routes.MapRoute( name: “Default”, url: “{controller}/{action}/{id}”, defaults: new { id = UrlParameter.Optional } ); 这个动作: [System.Web.Http.HttpPost] [System.Web.Http.ActionName(“GetLoginSeed”)] public object GetLoginSeed() [System.Web.Http.HttpPost] [System.Web.Http.AllowAnonymous] [System.Web.Http.ActionName(“Authenticate”)] public object PerformLogin(JObject jr) 这是post请求: http://localhost:61971/api/Login/GetLoginSeed 为什么我总是得到多个匹配请求错误的操作?

如何从ASP.NET Web API读取XML?

我有一个Web API,它将读取XML并将其传递给适当的模型进行处理。 我怎样才能收到即将发布的XML? 我应该使用哪种数据类型? 我是否使用StreamReader , StreamContent或XmlDocument或其他?

ASP.NET Web API跨请求缓存操作筛选器属性

在ASP.NET Web API(4.0.30506)中似乎有一些奇怪的行为,我以前没见过。 我所看到的是,相同的动作filter属性实例在Web API请求上被重用。 如果此属性将依赖项注入其中,则这尤其是一个问题,因为这些依赖项可能特定于Web请求。 我知道属性更好是被动的 ,但我的假设是动作filter属性没有被缓存。 我搜索了描述这个的任何文章,博客文章或微软更改日志及其背后的原因,但我找不到一件事。 这让我想知道我的配置是否有问题导致这种情况发生。 但是,我能够在一个新的,空的Visual Studio 2012 Web API项目中重现这个问题。 我所做的是使用带有“Web API”模板的Visual Studio 2012 ASP.NET MVC 4 Web应用程序项目创建一个新的空项目。 它附带了Web API 4.0.20710.0 NuGet包。 之后我添加了以下属性: [DebuggerDisplay(“{id}”)] public class TestFilterAttribute : System.Web.Http.Filters.ActionFilterAttribute { private static readonly List used = new List(); private static int counter; private readonly int id; public TestFilterAttribute() { this.id […]