C# 使用ViewComponent类的布线问题
我正在写一个叫做社交网络的项目。它是ASP.NET核心Web应用程序+模板Web应用程序(模型视图控制器) 我添加了一些功能,在这些功能中,如果用户获得授权,那么他将有一个“注销”字段,如果没有登录,则在页面C# 使用ViewComponent类的布线问题,c#,asp.net-core,routing,C#,Asp.net Core,Routing,我正在写一个叫做社交网络的项目。它是ASP.NET核心Web应用程序+模板Web应用程序(模型视图控制器) 我添加了一些功能,在这些功能中,如果用户获得授权,那么他将有一个“注销”字段,如果没有登录,则在页面“\u Layout.cshtml”上有一个“登录”。为此,我使用了ViewComponent 但路由没有正常工作。 当我点击“登录”时,我将被重定向到”../Account/~/Account/Login“,而不是”../Account/Login“,如下面的屏幕截图所示 屏幕截图: 文
“\u Layout.cshtml”
上有一个“登录”。为此,我使用了ViewComponent
但路由没有正常工作。
当我点击“登录”时,我将被重定向到”../Account/~/Account/Login“
,而不是”../Account/Login“
,如下面的屏幕截图所示
屏幕截图:文件
“\u Layout.cshtml”
的一部分:
MenuPartsViewComponent.cs:
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewComponents;
namespace SocialNetwork.Components
{
public class MenuPartsViewComponent : ViewComponent
{
public IViewComponentResult Invoke()
{
if (HttpContext.User.Identity.Name == null)
{
return new HtmlContentViewComponentResult(
new HtmlString(@"<li><a href=""~/Account/Login"" class=""navbar-brand"">Login</a></li>"));
}
else
{
return new HtmlContentViewComponentResult(
new HtmlString(@"<li><a href=""~/Account/Logout"" class=""navbar-brand"">Logout</a></li>"));
}
}
}
}
使用Microsoft.AspNetCore.Html;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.AspNetCore.Mvc.ViewComponents;
名称空间SocialNetwork.Components
{
公共类菜单PartsViewComponent:ViewComponent
{
公共IViewComponentResult调用()
{
if(HttpContext.User.Identity.Name==null)
{
返回新的HtmlContentViewComponentResult(
新的HtmlString(@“”);
}
其他的
{
返回新的HtmlContentViewComponentResult(
新的HtmlString(@“”);
}
}
}
}
问题在于您的href
属性值。将~
从的所有href
中删除);
问题在于,在cshtml中,当视图被渲染时,瓷砖~
会被转换。但是,在视图组件中,情况并非如此,因为您负责生成与视图相关的信息
使用生成所需的URL,而不是对其进行硬编码
public class MenuPartsViewComponent : ViewComponent {
public IViewComponentResult Invoke() {
if (HttpContext.User.Identity.Name == null) {
var login = Url.Action("Login", "Account");
var html = @"<li><a href=""{0}"" class=""navbar-brand"">Login</a></li>";
return new HtmlContentViewComponentResult(
new HtmlString(string.Format(html, login))
);
} else {
var logout = Url.Action("Logout", "Account");
var html = @"<li><a href=""{0}"" class=""navbar-brand"">Logout</a></li>";
return new HtmlContentViewComponentResult(
new HtmlString(string.Format(html, logout))
);
}
}
}
public类菜单partsviewcomponent:ViewComponent{
公共IViewComponentResult调用(){
if(HttpContext.User.Identity.Name==null){
var login=Url.Action(“登录”、“帐户”);
var html=@“”;
返回新的HtmlContentViewComponentResult(
新的HtmlString(string.Format(html,登录))
);
}否则{
var logout=Url.Action(“注销”、“帐户”);
var html=@“”;
返回新的HtmlContentViewComponentResult(
新的HtmlString(string.Format(html,注销))
);
}
}
}
@Anton Belesky,您在
行中也遇到了问题,这就是我开始回答这个问题的原因。两个答案都解决了您的问题。@TanvirArjel~
的_布局中的链接将按预期工作。视图引擎将呈现正确的URL。问题在于它们是硬编码的,并且没有t不要使用路由表。@Nkosi非常感谢您!是的!您是对的。
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewComponents;
namespace SocialNetwork.Components
{
public class MenuPartsViewComponent : ViewComponent
{
public IViewComponentResult Invoke()
{
if (HttpContext.User.Identity.Name == null)
{
return new HtmlContentViewComponentResult(
new HtmlString(@"<li><a href=""~/Account/Login"" class=""navbar-brand"">Login</a></li>"));
}
else
{
return new HtmlContentViewComponentResult(
new HtmlString(@"<li><a href=""~/Account/Logout"" class=""navbar-brand"">Logout</a></li>"));
}
}
}
}
return new HtmlContentViewComponentResult(
new HtmlString(@"<li><a href=""/Account/Login"" class=""navbar-brand"">Login</a></li>"));
public class MenuPartsViewComponent : ViewComponent {
public IViewComponentResult Invoke() {
if (HttpContext.User.Identity.Name == null) {
var login = Url.Action("Login", "Account");
var html = @"<li><a href=""{0}"" class=""navbar-brand"">Login</a></li>";
return new HtmlContentViewComponentResult(
new HtmlString(string.Format(html, login))
);
} else {
var logout = Url.Action("Logout", "Account");
var html = @"<li><a href=""{0}"" class=""navbar-brand"">Logout</a></li>";
return new HtmlContentViewComponentResult(
new HtmlString(string.Format(html, logout))
);
}
}
}