Url.Action Knockout + Asp.Net MVC Core 2.2 错误
Url.Action is wrong with Knockout + Asp.Net MVC Core 2.2
我正在尝试生成一个 table,其中每行包含一个 link,带有敲除和 Asp.Net Core 2.2。实际上我希望整行稍后都可以点击,但是对于这个例子我将它添加为 table 字段。
@Url.Action("MyAction", "My")
的路径生成错误。他们两个都完全是空的。控制器和动作都没有出现在生成的 link.
中
<tbody data-bind="foreach: searchResults">
<tr>
<td data-bind="text: id"></td>
<td>
<a data-bind="attr: { href: '@Url.Action("MyAction", "My")/' + id}">
Click
</a>
</td>
<td data-bind="text: name"></td>
<td data-bind="text: date"></td>
</tr>
</tbody>
"My"是控制者"MyController"。 "MyAction" 是我想在点击时调用的操作方法的名称。我有另一个控制器,用于不同的路径。我正在尝试使用的是这个:
[Route("api/[controller]")]
[ApiController]
public class MyController : ControllerBase
{
private readonly IWebServiceFactory webServiceFactory;
public MyController(IWebServiceFactory webServiceFactory)
{
this.webServiceFactory = webServiceFactory;
}
// GET: api/MyAction/{GUID}
[HttpGet("{id:Guid}")]
public async Task<IActionResult> MyAction(Guid id)
{
var result = await webServiceFactory.GetWebService().GetFileAsync(id);
var memory = new MemoryStream();
await result .Stream.CopyToAsync(memory);
memory.Position = 0;
return File(memory, "application/unknown", result.FileName);
}
}
我想可能是因为控制器路由没有使用任何动作,但是即使在路由中添加了动作,结果也是一样的。
我在 Startup.cs
中将 Mvc 与默认路由一起使用:
app.UseMvcWithDefaultRoute();
这是我得到的实际输出:
<a data-bind="attr: { href: '/' + id }" href="/173a4cec-a258-4ebb-d28e-08d6cee0966c">Click</a>
我做错了什么?
避免 Url.Action
尝试呈现与 url 相关的 MVC 视图而不是 Api 端点 url,
将 @Url.RouteUrl
与已应用于给定 Api 控制器操作方法的 route-name 一起使用;例如。 ApiMyMyActionGuidRoute
[Route("api/[controller]")]
[ApiController]
public class MyController : ControllerBase
{
[HttpGet("{id:Guid}", Name ="ApiMyMyActionGuidRoute")]
public async Task<IActionResult> MyAction(Guid id)
{
// Your implementation goes here ...
}
}
在您看来,通过 @Url.Route
渲染 url。
请注意,您必须为 id
参数应用虚拟 Guid
值,因为此参数在操作方法 (MyAction(Guid id)
).
中是必需的
由于此 id
值是通过 Knockout
绑定应用的,因此在将其应用到 Knockout
data-bind
表达式。
@{
var routeUrl = Url.RouteUrl("ApiMyMyActionGuidRoute", new { id = Guid.Empty });
// routeUrl = "/api/My/00000000-0000-0000-0000-000000000000"
routeUrl = routeUrl.Substring(0, routeUrl.LastIndexOf("/"));
// routeUrl = "/api/My"
}
<p><a data-bind="attr: { href: '@routeUrl/' + id }">Click</a></p>
这将生成下面的 html 标记,该标记具有正确的 Api 端点 url,Knockout
将在其上应用 id
参数值。
<p><a data-bind="attr: { href: '/api/My/' + id }">Click</a></p>
我正在尝试生成一个 table,其中每行包含一个 link,带有敲除和 Asp.Net Core 2.2。实际上我希望整行稍后都可以点击,但是对于这个例子我将它添加为 table 字段。
@Url.Action("MyAction", "My")
的路径生成错误。他们两个都完全是空的。控制器和动作都没有出现在生成的 link.
<tbody data-bind="foreach: searchResults">
<tr>
<td data-bind="text: id"></td>
<td>
<a data-bind="attr: { href: '@Url.Action("MyAction", "My")/' + id}">
Click
</a>
</td>
<td data-bind="text: name"></td>
<td data-bind="text: date"></td>
</tr>
</tbody>
"My"是控制者"MyController"。 "MyAction" 是我想在点击时调用的操作方法的名称。我有另一个控制器,用于不同的路径。我正在尝试使用的是这个:
[Route("api/[controller]")]
[ApiController]
public class MyController : ControllerBase
{
private readonly IWebServiceFactory webServiceFactory;
public MyController(IWebServiceFactory webServiceFactory)
{
this.webServiceFactory = webServiceFactory;
}
// GET: api/MyAction/{GUID}
[HttpGet("{id:Guid}")]
public async Task<IActionResult> MyAction(Guid id)
{
var result = await webServiceFactory.GetWebService().GetFileAsync(id);
var memory = new MemoryStream();
await result .Stream.CopyToAsync(memory);
memory.Position = 0;
return File(memory, "application/unknown", result.FileName);
}
}
我想可能是因为控制器路由没有使用任何动作,但是即使在路由中添加了动作,结果也是一样的。
我在 Startup.cs
中将 Mvc 与默认路由一起使用:
app.UseMvcWithDefaultRoute();
这是我得到的实际输出:
<a data-bind="attr: { href: '/' + id }" href="/173a4cec-a258-4ebb-d28e-08d6cee0966c">Click</a>
我做错了什么?
避免 Url.Action
尝试呈现与 url 相关的 MVC 视图而不是 Api 端点 url,
将 @Url.RouteUrl
与已应用于给定 Api 控制器操作方法的 route-name 一起使用;例如。 ApiMyMyActionGuidRoute
[Route("api/[controller]")]
[ApiController]
public class MyController : ControllerBase
{
[HttpGet("{id:Guid}", Name ="ApiMyMyActionGuidRoute")]
public async Task<IActionResult> MyAction(Guid id)
{
// Your implementation goes here ...
}
}
在您看来,通过 @Url.Route
渲染 url。
请注意,您必须为 id
参数应用虚拟 Guid
值,因为此参数在操作方法 (MyAction(Guid id)
).
由于此 id
值是通过 Knockout
绑定应用的,因此在将其应用到 Knockout
data-bind
表达式。
@{
var routeUrl = Url.RouteUrl("ApiMyMyActionGuidRoute", new { id = Guid.Empty });
// routeUrl = "/api/My/00000000-0000-0000-0000-000000000000"
routeUrl = routeUrl.Substring(0, routeUrl.LastIndexOf("/"));
// routeUrl = "/api/My"
}
<p><a data-bind="attr: { href: '@routeUrl/' + id }">Click</a></p>
这将生成下面的 html 标记,该标记具有正确的 Api 端点 url,Knockout
将在其上应用 id
参数值。
<p><a data-bind="attr: { href: '/api/My/' + id }">Click</a></p>