所以我需要将一个组合序列化为JSON(使用JSON.NET),并且希望能够快速获得这个问题.
我有一个非常基本的复合实现,我只是试图用来支撑我的服务和数据结构,但JSONSerializer只是序列化根节点.
码:
namespace Data
{
public abstract class Element
{
protected string _name;
public Element(string name)
{
_name = name;
}
public abstract void Add(Element element);
public string Name { get { return _name; } }
}
public class ConcreteElement : Element
{
public ConcreteElement(string name) : base(name) { }
public override void Add(Element element)
{
throw new InvalidOperationException("ConcreteElements may not contain Child nodes. Perhaps you intended to add this to a Composite");
}
}
public class Composite: Element …Run Code Online (Sandbox Code Playgroud) 我有一个storeprocdure返回集合的情况,但我没有如何对象结构,因为查询是非常动态的.
一个查询可以返回:
Id | 位置| MarketSegment | ...... n列
另一个可以回来
Id | 销售代表| 位置| 地区| ...... n列
我只是简单地返回一个"对象",如下面的代码所示.我知道这不会起作用,但我怎么设置它呢?
using (DbContext db = new Context())
{
var items = db.Database.SqlQuery<object>(
"SP @Param1, @Param2",
new SqlParameter("Param1", ped),
new SqlParameter("Param2", 25)
).ToList();
return Request.CreateResponse<List<object>>(HttpStatusCode.OK, items);
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我不知道是否显示SP会有所帮助,除非我能解释得更多.
每列都表示为自定义字段.用户可以创建n个自定义字段.因此,如果您为User1运行SP并且他有5个自定义字段,则每个自定义字段将在列中表示,但如果User2有3个自定义字段,则仅表示3列.我无法控制的是自定义字段名称和自定义字段数.
我有以下简单的控制台应用程序使用Owin SelfHost托管webapi.来自控制台本身的响应有效,但如果从fiddler尝试我只是得到与localhost的连接失败.(与浏览器相同).
class Program
{
static void Main()
{
string baseAddress = "http://localhost:34300/";
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("da-dk");
// Start OWIN host
using (WebApp.Start<Startup>(url: baseAddress))
{
// Create HttpCient and make a request to api/values
HttpClient client = new HttpClient();
var response = client.GetAsync(baseAddress + "api/GpsPositions").Result;
Console.WriteLine(response);
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
Console.WriteLine("Listening on " + baseAddress);
}
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
我的客户想要通过asp.net Web API发送电子邮件.我是Web服务的新手,对此有0知识,请指导我如何完成此任务,如果有人可以提供代码,必须有一些像这样的Web服务.
using (MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text))
{
mm.Subject = txtSubject.Text;
mm.Body = txtBody.Text;
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = true;
smtp.Send(mm);
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
}
Run Code Online (Sandbox Code Playgroud) 我们的网站有一个REST API.我们一直在通过为端点添加端点添加或更新端点的发布版本来对端点进行版本控制.例如
这种方法的问题是我们的客户端代码调用了不同版本的端点.例如,页面可以调用/ v3/service-c和/ v1/service-a.
我想设置它,以便我们的开发人员可以通过在端点前添加最新版本的端点来访问最新版本的API.使用上面的示例,页面将调用/ v3/service-c和/ v3/service-a,对于service-a,请求将被转发到绑定到/ v1/service-a的操作,因为是/ v3之前的最新服务版本.
我知道我可以在代码中明确地手动添加路由,但是管理不同版本的端点会变得很困难.
我知道如何枚举路由表中的版本并解析它们; 这部分就解决了.但是,我不确定的是我究竟是如何截取路由的,以便可以将对/ v3/-service-a的调用转发到我为/ v1/service-a设置的路由.
有任何想法吗?
我试图通过WebApi方法将XML反序列化为对象。
我有以下课程:
[XmlRoot(IsNullable = false)]
public class MyObject
{
[XmlElement("Name")]
public string Name {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
以及WebApi控制器中的以下方法。
[HttpPost]
public HttpResponseMessage UpdateMyObject(MyObject model)
{
//do something with the model
}
Run Code Online (Sandbox Code Playgroud)
我XmlSerializer通过在Web项目的启动中设置以下内容来使用:
config.Formatters.XmlFormatter.UseXmlSerializer = true;
Run Code Online (Sandbox Code Playgroud)
当我发布以下XML时,model会正确反序列化,并且我可以读取其属性。
<?xml version="1.0" encoding="UTF-8"?>
<MyObject>
<Name>HelloWorld</Name>
</MyObject>
Run Code Online (Sandbox Code Playgroud)
但是,当我使用DOCTYPE声明发布XML时,该model值为null,并且似乎在方法输入时未反序列化。也就是说,此XML不会反序列化为模型:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE MyObject SYSTEM "http://example.com/MyObject.dtd">
<MyObject>
<Name>HelloWorld</Name>
</MyObject>
Run Code Online (Sandbox Code Playgroud)
希望有人能够提供帮助。
我将ASP.NET MVC应用程序与ASP.NET WebApi应用程序一起托管,localhost并且localhost/api当ASP.NET WebApi的一个操作调用时,我不想设置cookie .
我试着像这样设置我的cookie:
response.Headers.AddCookies(new[]
{
new CookieHeaderValue("test", "asd")
{
Expires = DateTime.Now.AddDays(1),
Domain = Request.RequestUri.Host,
Path = "/",
HttpOnly = false
}
});
Run Code Online (Sandbox Code Playgroud)
我在响应标题中得到了:
Set-Cookie:test=asd; expires=Thu, 30 Oct 2014 09:53:35 GMT; domain=localhost; path=/
Run Code Online (Sandbox Code Playgroud)
但是那个cookie不是出于某种原因而创建的.
出于测试目的,我尝试从ASP.NET MVC应用程序中设置cookie,如下所示:
HttpContext.Response.Cookies.Add(new HttpCookie("test", "asd")
{
Expires = DateTime.Now.AddDays(1),
Domain = HttpContext.Request.Url.Host,
Path = "/",
HttpOnly = false
});
Run Code Online (Sandbox Code Playgroud)
响应标头包含:
Set-Cookie:test=asd; domain=localhost; expires=Thu, 30-Oct-2014 09:56:08 GMT; path=/
Run Code Online (Sandbox Code Playgroud)
但我还是看不到那个饼干.我做错了怎么可能这些标题被忽略了?但是,其他cookie工作正常(例如.ASPXAUTH).
我正在尝试使用Asp.Net Web Api创建一个通过Post上传文件的Web服务.这些分别是Client和Web Api的实现:
客户:
using (var client = new HttpClient()) {
client.BaseAddress = new Uri("https://127.0.0.1:44444/");
using (var content =
new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture))) {
content.Add(new ByteArrayContent(File.ReadAllBytes(filepath)));
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
var response = await client.PostAsync("api/Synchronization", content);
if (response.IsSuccessStatusCode)
eventLog1.WriteEntry("Synchronization has been successful", EventLogEntryType.Information);
else
eventLog1.WriteEntry(response.StatusCode + ":" + response.ReasonPhrase, EventLogEntryType.Error);
}
}
Run Code Online (Sandbox Code Playgroud)
服务器:
public class SynchronizationController : ApiController {
public HttpResponseMessage SynchronizeCsv() {
var task = this.Request.Content.ReadAsStreamAsync();
task.Wait();
Stream requestStream = task.Result;
try {
Stream fileStream = File.Create(HttpContext.Current.Server.MapPath(path));
requestStream.CopyTo(fileStream); …Run Code Online (Sandbox Code Playgroud) 我创建了以下异步web-api-controller操作,但它永远不会返回
public async Task<string> Tester()
{
Task t = new Task(() => Thread.Sleep(2000));
await t;
return "OK";
}
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
我可能在这里缺少明显的东西。我是MVC和Web API的新手,所以我正在努力保持头脑清醒。
我有一个与Web API服务接口的MVC应用程序。身份验证将由内部开发的登录服务处理。在工作时,MVC客户端应检查当前用户是否已通过身份验证。如果不是,则它将重定向到该登录服务,该登录服务应该对用户进行身份验证并更新当前用户。然后,我需要能够从Web API服务访问此身份。
我在假设MVC应用程序中的当前主体(通过Thread.CurrentPrincipal或HTTPContext.Current.User设置)应该在我的Web API服务中可用,但是每当我尝试从该服务访问它时,主体是空的。我尝试使用以下所有选项从服务访问委托人,但始终为空:
RequestContext.Principal
User.Identity
HttpContext.Current.User
Thread.CurrentPrincipal
这是我的代码的基本概念:
MVC Controller:
public ActionResult Index() {
//Just create a test principal here to see if it's available in the service
IPrincipal temp = new GenericPrincipal(new GenericIdentity("myUserName"), new string[]{});
Thread.CurrentPrincipal = temp;
using (var client = new HttpClient()) {
client.BaseAddress = new Uri("myServiceAddress");
HttpResponseMessage response = client.GetAsync("resourceString")).Result;
...Code to deal with result
}
}
Web API Controller:
[HttpGet]
public HttpResponseMessage MyAction() {
if (User.Identity == null || !User.Identity.IsAuthenticated) { …Run Code Online (Sandbox Code Playgroud) asp.net-web-api ×10
c# ×7
asp.net-mvc ×3
asp.net ×2
async-await ×1
cookies ×1
dbcontext ×1
email ×1
httpclient ×1
json ×1
json.net ×1
owin ×1
principal ×1
sql ×1
xml ×1