我想告诉消费者我的api关于新创建的对象的位置.我知道有Created()
CreatedAtRoute()
,CreatedAtAction()
但我不确定如何使用它.
这是我尝试过的:
我有一个Get资源,我想指出.它需要一个ID作为输入:
[HttpGet("/${id}", Name = "GetProduct")]
[ProducesResponseType(typeof(Produkt), 200)]
public IActionResult Get([FromRoute] int id)
{
// some code...
return Ok(...);
}
Run Code Online (Sandbox Code Playgroud)
当通过我的POST路由创建产品时,我想通过Location标头指向此资源:
尝试1
[HttpPost]
[ProducesResponseType(typeof(Produkt), 200)]
public IActionResult CreateNewProduct([FromBody] ProduktDtoForCreate productFromBody)
{
//...
return CreatedAtRoute("GetProduct", new { id = productToCreate.Id }, productToCreate);
}
Run Code Online (Sandbox Code Playgroud)
这将返回一个位置标头:http:// localhost:5000 / $ 15003
尝试2
[HttpPost]
[ProducesResponseType(typeof(Produkt), 200)]
public IActionResult CreateNewProduct([FromBody] ProduktDtoForCreate productFromBody)
{
//...
return Created(new Uri($"{Request.Path}/{productToCreate.Id}", UriKind.Relative), productToCreate);
}
Run Code Online (Sandbox Code Playgroud)
这个工作并返回/api/v1.0/produkte/16004但似乎不应该使用当前请求指向新位置.另外我不确定这是不是很好的做法?
我很难尝试用 Jest 测试角度组件。
我有这个组件:
媒体图像.component.ts
import { Component, Input } from '@angular/core'
import { SanityService } from '@services/sanity/sanity.service'
import Waypoint from '@interfaces/waypoint'
@Component({
selector: 'app-media-image',
templateUrl: './media-image.component.html',
styleUrls: ['./media-image.component.scss']
})
export class MediaImageComponent {
@Input() mediaData: Waypoint = null
constructor(private _sanity: SanityService) {}
imageUrl(source: any) {
return this._sanity.urlFor(source)
}
}
Run Code Online (Sandbox Code Playgroud)
模板中调用imageUrl
该组件需要 SanityService
理智服务.ts
import { Injectable } from '@angular/core'
import { environment } from '@environments/environment'
import imageUrlBuilder from '@sanity/image-url'
import sanityClient from '@sanity/client'
@Injectable({
providedIn: 'root'
})
export class …
Run Code Online (Sandbox Code Playgroud) 我试图在日期类型的输入中区分鼠标和键盘输入:
模板
<input type="date" (change)="onChange($event)">
Run Code Online (Sandbox Code Playgroud)
控制器
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
onChange(event) {
// How to check if user used the mouse to select a date
// or if he typed a date
console.log(event);
}
}
Run Code Online (Sandbox Code Playgroud)
最简单的 stackblitz:https ://stackblitz.com/edit/angular-zz2nbl
是否可以?我需要订阅多个不同的事件吗?