在Web API 2.1中,我使用以下命令注册BSON:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Formatters.Add(new BsonMediaTypeFormatter());
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何注册格式化程序.Net Core Web API.似乎肯定它会在Startup.Configuration中,但我仍然是Core的新手,不知道如何添加这种类型的格式化程序.
我创建了一个Web API,并且之前运行良好。尝试浏览API时,突然出现以下错误。
Cannot add duplicate collection entry of type 'add' with unique key attribute 'name' set to 'Access-Control-Allow-Origin'
Config Source:
30: <customHeaders>
31: <add name="Access-Control-Allow-Origin" value="*"/>
32: <add name="Access-Control-Allow-Headers" value="Content-Type"/>
我在web.config中有以下代码
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type"/>
</customHeaders>
Run Code Online (Sandbox Code Playgroud)
任何人都可以建议我这里的问题,因为它以前工作正常。
我正在使用RESTful服务,并发现Postman是GET,POST和测试API的最佳插件之一.
我在邮递员中找到了Basic Auth,No Auth,DIgest Auth,OAuth,AWS.如何测试授权控制器和方法.
我知道授权属性检查 user.Identity.IsAuthenticated
我不确定如何使用Postman在具有特定角色的控制器和方法中传递授权
[Authorize(Roles = "Admin, Super User")]
public ActionResult AdministratorsOnly()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
这是我的启动文件
public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
public static string PublicClientId { get; private set; }
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context and user manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
// Enable the application to use a cookie to …Run Code Online (Sandbox Code Playgroud) 我在https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-vsc上尝试dotnet核心教程
TodoContext.cs
using Microsoft.EntityFrameworkCore;
namespace TodoApi.Models
{
public class TodoContext : DbContext
{
public TodoContext(DbContextOptions<TodoContext> options)
: base(options)
{
}
public DbSet<TodoItem> TodoItems { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
Startup.cs
using ...
using TodoApi.Models;
using Microsoft.EntityFrameworkCore;
namespace TodoApi
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) …Run Code Online (Sandbox Code Playgroud) 我有一个webapi项目,我正在利用swashbuckle框架来清除api文档.
我已按照指示使用我的控制器和DTO构建文档xml文件,并且它在本地运行良好.
但是,在生成swagger文档时,会引发500错误.我已经确认是否删除了我的xml注册行,生成了swagger doc并成功返回.
这是我的注册行:
GlobalConfiguration.Configuration.EnableSwagger(c =>
{
...
c.IncludeXmlComments($"{System.AppDomain.CurrentDomain.BaseDirectory}bin\\Company.MyApp.xml");
...
Run Code Online (Sandbox Code Playgroud)
更新:我做了一些额外的日志记录,虽然IncludeXmlComments的这个lne在启动时成功运行,但当我从服务器请求swagger.json文件时,我得到一个System.IO.FileNotFoundException: Could not find file 'D:\home\site\wwwroot\bin\Monetary.Scheduling.xml'例外.当我使用Kudu工具查看这个目录时,我找不到这个文件.
TL; DR:为什么这个文件在本地显示正常,但是当我使用Kudu或Octopus nugget包部署到Azure时,这个文件不存在?
我有WebAPI控制器从表数据生成XML
这是代码
public class XMLController : ApiController
{
private trackingappEntities db = new trackingappEntities();
[HttpGet]
public HttpResponseMessage Index()
{
var xdoc = new XDocument(
new XElement("data",
db.TimeTables.Select(w =>
new XElement("worker",
new XAttribute("id", w.INN),
new XElement("start", w.StartDay),
new XElement("pause", w.StartPause),
new XElement("continue", w.EndPause),
new XElement("end", w.EndDay)
)
)
)
);
return new HttpResponseMessage() { Content = new StringContent(xdoc.ToString(), Encoding.UTF8, "application/xml") };
}
}
Run Code Online (Sandbox Code Playgroud)
这是TimeTable类
public partial class TimeTable
{
public int Id { get; set; }
public string Company { get; …Run Code Online (Sandbox Code Playgroud) 使用IoC可解析接口来指定DTO有什么价值吗?
Fer示例:
private readonly IGetTransactionsQuery _query;
private readonly ICreateTransactionCommand _createCommand;
public TransactionsController(
IGetTransactionsQuery query,
ICreateTransactionCommand createCommand)
{
_query = query;
_createCommand = createCommand;
}
[EnableQuery]
public IQueryable<ITransactionQueryModel> Get()
{
return _query.Execute();
}
public async Task<IHttpActionResult> Post(ICreateTransactionModel transaction)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
await _createCommand.Execute(transaction);
return Created(transaction);
}
Run Code Online (Sandbox Code Playgroud)
在这里,我使用的是ITransactionQueryModel和ICreateTransactionModel,而不是结核.这里有商业价值吗?哪种方案可以从这种方法中受益?我可以想到一些,但希望得到一些用例场景的共识
注意:我只是指控制器"动作"方法,而不是构造函数,因为IoC的好处是显而易见的
我有一个POST方法,它将返回用户的项目列表,因为我是c#web api的新手,如果Id为空,空或无效,我很难设置正确的条件和响应.我尝试过类似的响应并且它不起作用主要是因为这些示例使用的是iHttpActionResult而不是List <>
这是我的控制器中的代码,我不确定在我提供的评论中放置什么:
[HttpPost]
public List<ValueStory> UserValueStories ([FromBody] ValueStory valuestory)
//public void UserValueStories([FromBody] ValueStory Id)
{
if (valuestory.Id == "" || valuestory.Id == null)
{
//what code to add to change status code to 400 and to display error message?
}
//what code to put if the id is not valid, what status code and what message?
var valueStoryName = (from vs in db.ValueStories
where vs.Id == valuestory.Id
select vs).ToList();
List<ValueStory> vs1 = new List<ValueStory>();
foreach (var v in …Run Code Online (Sandbox Code Playgroud) 我正在为我的Web api项目使用简单的注入器。我有一个需要会话令牌才能实例化的服务。
public class CustomerService
{
public CustomerService(Auth auth, IRepositoryFactory repositoryFactory)
{
// make post call to another web api for validation
SomeWebApiCallToValidateAuth.vaildate(auth);
}
}
Run Code Online (Sandbox Code Playgroud)
因此,对于此服务,它需要一个auth令牌和一个repositoryFactory。我希望它能够注入auth参数(来自http Web请求),并同时使用注册到容器的指定已实现多数民众赞成解决存储库工厂。
但是我不确定如何在简单的注射器上注册它,或者是否有解决方法。任何帮助都会很棒。谢谢。
我写了一个存储过程: -
CREATE PROCEDURE GET_USERSTORYDATA_EXISTS
@MyWizardUserStoryId INT
,@ProjectId INT
,@StoryTitle NVARCHAR(MAX)
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS (SELECT 1 FROM USERSTORY WHERE [MyWizardUserStoryId]=@MyWizardUserStoryId AND ProjectId=@ProjectId)
BEGIN
IF EXISTS (SELECT 1 FROM USERSTORY WHERE [MyWizardUserStoryId]=@MyWizardUserStoryId AND ProjectId=@ProjectId AND Title=@StoryTitle)
BEGIN
SELECT 0;
END
ELSE
BEGIN
SELECT 1;
END
END
ELSE
BEGIN
SELECT 1;
END
END
GO
Run Code Online (Sandbox Code Playgroud)
我通过Entity框架使用了这个程序: -
List<Int32>lst=context.GET_USERSTORYDATA_EXISTS(MyWizardUserStoryId,ProjectId,storyTitle);
Run Code Online (Sandbox Code Playgroud)
但它给我的错误: -
Can not implicitly convert type 'ObjectResult<int?>' to List<int>
Run Code Online (Sandbox Code Playgroud) asp.net-web-api ×10
c# ×7
asp.net-core ×2
.net ×1
asp.net ×1
asp.net-mvc ×1
bson ×1
json ×1
linq ×1
postman ×1
swagger ×1
swashbuckle ×1