标签: asp.net-web-api

从Web Api Controller返回XML

如何从web api控制器指定return xml?在一个浏览器中,数据以XML格式打开,在另一个浏览器中以JSON格式打开.

编辑

这是我的行动:

    [HttpGet]
    public IEnumerable<MagazineMeta> GetLastUploadPdfMeta(int count)
    {
       List<MagazineMeta> metas = _metaRepository
                 .GetAll()
                 .OrderBy(e => e.TimeAdd)
                 .Take(count)
                 .ToList();

       return metas;
    }
Run Code Online (Sandbox Code Playgroud)

c# xml asp.net-mvc asp.net-web-api

-1
推荐指数
1
解决办法
1万
查看次数

如何通过AJAX调用的成功回调替换页面的整个HTML或其正文内容?

我需要替换页面的整个HTML或其正文内容(而不是将更多html附加到现有内容).

接受答案在这里教我如何返回数据,但加入到"身体"不相当的工作.这是我现在拥有的jQuery:

<script>
    $(document).ready(function () {
        $("#btnGetData").click(function () {
            document.body.style.cursor = 'wait';
            $.ajax({
                type: 'GET',
                url: '@Url.RouteUrl(routeName : "QuadrantData", routeValues : new { httpRoute = true , unit = "ABUELOS", begdate = "2016-08-20", enddate = "2016-08-27"  })',
                contentType: 'text/plain',
                cache: false,
                xhrFields: {
                    withCredentials: false
                },
                success: function (returneddata) {
                    $("body").remove;
                    $("body").append($(returneddata));
                },
                error: function () {
                    console.log('hey, boo-boo!');
                }
            }); // ajax
            document.body.style.cursor = 'pointer';
        }); // button click
    }); // ready
</script>
Run Code Online (Sandbox Code Playgroud)

...所以你可以看到我试图先删除正文中的html,然后将返回的数据附加到正文中.

这个REST方法返回我想要的html: …

javascript jquery replaceall replacewith asp.net-web-api

-1
推荐指数
1
解决办法
877
查看次数

如何将模型类对象转换为数据表

我是 c# 新手,我想在 Web API 中将模型类对象转换为 DataTable。

模型类

 public class Employee_Master_Model
    {
        public string UserType { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string MiddleName { get; set; }
        public string Mobile { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public bool IsActive { get; set; }
        public int EmployeeId { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

控制器:

 public async Task<IActionResult> …
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net-web-api asp.net-core

-1
推荐指数
1
解决办法
1万
查看次数

在 Core 2.2 中返回异步结果时无法使用 Ok(...) 作为状态代码

我搭建了一个像这样的 WebAPI 模板。

[HttpGet]
public async Task<ActionResult<IEnumerable<ThingVm>>> GetThings()
{
  Task<List<ThingVm>> output = Context.Things
    .Select(e => new ThingVm(e))
    .ToListAsync();
  return await output;
}
Run Code Online (Sandbox Code Playgroud)

我注意到返回的结果不会传达任何状态代码(因为我没有提供 200 或 404 等)。所以我添加了标准的、通常的Ok(...)调用。

[HttpGet]
public async Task<ActionResult<IEnumerable<ThingVm>>> GetThings()
{
  ...
  return await Ok(output);
}
Run Code Online (Sandbox Code Playgroud)

遗憾的是,计算机并没有意识到该操作并像这样解释了错误。

CS1061“OkObjectResult”不包含“GetAwaiter”的定义,并且找不到接受“OkObjectResult”类型的第一个参数的可访问扩展方法“GetAwaiter”(您是否缺少 using 指令或程序集引用?)

我明白为什么会这样。我不知道如何在保留响应中发出的状态代码的同时解决它。

我在谷歌上搜索了它,但发现了一些非常陈旧的东西完全不相关的东西相当不相关的东西。就是这样 - 根本没有太多其他热门歌曲,这也令人惊讶。

如何发出状态代码并应用异步方法?它们不是相互排斥的,是吗?

c# asynchronous asp.net-web-api asp.net-core asp.net-core-2.2

-1
推荐指数
1
解决办法
1229
查看次数

“Task&lt;User&gt;”不包含“LastActive”的定义

这是我的 .cs 代码,其中正在生成错误(第 23 行):“Task”不包含“LastActive”的定义,并且无法找到接受“Task”类型的第一个参数的可访问扩展方法“LastActive”

using System.Security.Claims;
using System.Threading.Tasks;
using DatingApp.Data;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
using DatingApp.Models;
namespace DatingApp.Helper
{
    public class LogUserActivity : IAsyncActionFilter
    {
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var resultContext = await next();

            var userId = int.Parse(resultContext.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);

            var repo = resultContext.HttpContext.RequestServices
                .GetService<IUserRepository>();

            var user = repo.GetUserById(userId);

            user.LastActive= DateTime.Now;
            await repo.SaveAll();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的存储库

 public async Task<User> GetUserById(int id)
        {
            var user =await  _context.Users.Include(p=>p.Photos).FirstOrDefaultAsync(u=>u.Id==id);
            return user;
        }
Run Code Online (Sandbox Code Playgroud)

c# asp.net-web-api asp.net-core

-1
推荐指数
1
解决办法
62
查看次数

使用R调用Web服务:发送数据并将结果表返回到R中

http://snomedct.t3as.org/这是一个Web服务,可以分析英文临床文本,并报告任何可以检测到的概念.
对于例如 - 我有头痛.它会将头痛识别为症状.

现在我想做的是通过R将句子发送到Web服务,并将表格从网页返回到R进行进一步分析.

web-services r rcurl asp.net-web-api httr

-2
推荐指数
1
解决办法
6226
查看次数

String.Format 带有包含 html 的字符串

我正在开发一个从数据库中获取电子邮件模板然后发送它们的应用程序。

来自数据库的模板的正文有 html,我如何在这个字符串上做一个 String.Format ?目前我收到一条错误消息,说输入字符串的格式不正确。

    <!DOCTYPE html>
<html>
<head>
</head>
<body>
    <style>
        .container {
            font-size: 0;
        }

            .container.p.span {
                /*font-size: 16px;**/
            }

        .titleTextSpan {
            color: white;
            width: inherit;
            height: inherit;
            font-family: sans-serif, Arial;
            font-size: 20px;
            display: table-cell;
            vertical-align: middle;
            text-align: left;
        }
    </style>
    <div class="container" style="width:700px;">
        <div style="display:inline-block;">
        </div>
        <div style="width:700px;height:44px; background:#36A6DE;display:table;">
            <span class="titleTextSpan">PRODUCT NAME</span>
        </div>
        <div style="font-size:10pt;margin-left:20px;font-family:Arial, sans-serif;color:#4D4C4C">
            <br />
            Dear {16},
            <br /><br />
            Thank you for your request for PRODUCT NAME, which is now with your organisation …
Run Code Online (Sandbox Code Playgroud)

c# sql database sql-server asp.net-web-api

-2
推荐指数
1
解决办法
5791
查看次数

如何在c#中对json对象的数组进行deserilize

嗨,我想从Web服务返回的JSON对象中读取Id:

[{ "ID":11 "用户名": "TEST5"}]

码:

HttpResponseMessage response = null;

using (response = httpClient.PostAsync(url, content).Result)
{
    try
    {
        response.EnsureSuccessStatusCode(); //200 Ok
        string responJsonText = response.Content.ReadAsStringAsync().Result;
        JObject res = JObject.Parse(responJsonText);
       //if ((int)res["id"][0] > 0)
       // {
            var uid = (int)res["id"][0];
            if (uid > 0)
            {
                return uid;
            }
            else
            { return 0; }
       // }
       // else
       //{ return 0; }
    } 
Run Code Online (Sandbox Code Playgroud)

我如何使用C#从Json Response读取Id和用户名?

我收到以下消息的异常:

从JsonReader读取JObject时出错.当前的JsonReader项不是对象:StartArray.路径

.net c# json asp.net-web-api

-2
推荐指数
1
解决办法
115
查看次数

从另一个 API 控制器调用 API 控制器

注意:请不要将其写为重复。请通读我的完整问题。

我有 2 个 Web API 控制器 A 和 B。我需要在控制器 A 的一个方法中从控制器 B 调用一个方法。在执行一些操作之后,我需要它发生。

class AController : ApiController{
    public IHttpAction SomeMethod(){
        //some action I need to finish before the call for Controller B

        //Call for Controller B
    }
}

class BController : ApiController{
    public IHttpActionResult AnotherMethod(){
        //Some code that I want to be executed after the code in Controller A
    }
}
Run Code Online (Sandbox Code Playgroud)

请不要建议我创建另一个具有通用功能的类,这是我在寻找解决方案时经常在网络上找到的解决方案。我没有通用的功能。我只想以线性方式执行代码。一个接一个地。希望我已经清楚了。任何帮助,将不胜感激。

c# asp.net-mvc asp.net-mvc-4 asp.net-web-api

-3
推荐指数
1
解决办法
6229
查看次数

无法隐式转换类型'System.Collections.Generic.List <>'

我正在使用VS 2017来开发我的.NET应用程序.我编写了以下代码来从api中的表中检索数据

[HttpGet]
[Route("Index")]
public IEnumerable<Strings> Index()
{
    var list = db.Strings.Select(x => new { x.Iid, x.Value, x.Description, x.Itype }).ToList();
    return list;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

错误CS0266无法将类型'System.Collections.Generic.List <>'隐式转换为'System.Collections.Generic.IEnumerable'.存在显式转换(您是否错过了演员?)**

如何铸造是可能的?我已将IEnumerable更改为IList但问题仍然存在.我不想为此创建ViewModel.

c# linq asp.net-web-api

-3
推荐指数
1
解决办法
3181
查看次数