Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 主控制器中每个mvc操作结果的代码相同_C#_Html_Asp.net Mvc_Razor - Fatal编程技术网

C# 主控制器中每个mvc操作结果的代码相同

C# 主控制器中每个mvc操作结果的代码相同,c#,html,asp.net-mvc,razor,C#,Html,Asp.net Mvc,Razor,因此,我有一些通用的actionresults,暂时链接到各种视图。布局页面包含对adfs的调用,以填充每个页面必须具有的登录用户名。看起来像这样: <div class="float-right"> <section id="login"> Hello, <span class="username">@ViewBag.GivenName @ViewBag.LastN

因此,我有一些通用的actionresults,暂时链接到各种视图。布局页面包含对adfs的调用,以填充每个页面必须具有的登录用户名。看起来像这样:

            <div class="float-right">
                <section id="login">
                   Hello, <span class="username">@ViewBag.GivenName @ViewBag.LastName</span>!
                </section>
            </div>
但正如前面提到的,我需要在用户访问每个链接(actionresult)时显示它。因此,为了实现这一点,我必须将上面的所有代码发布到每个actionresult中

是否有某种方法可以将其作为一个整体应用于每个actionresult,而不必将代码从一个操作复制到另一个操作?我只是尝试注册到我的_Layout.cshtml的actionresult中,并调用partialview,但这并没有给我带来好的结果。我确信这是我错过的简单的东西


希望你们中的一些人能帮忙。非常感谢。

您可以创建一个基本控制器,并使所有控制器从中继承。将设置给定和姓氏的代码移动到一个单独的、受保护的方法中,并在需要时调用它。我认为您可以在基本控制器的
Initialize
方法中调用该函数。这样,您就不需要将其直接调用到操作中。
您还可以建立一个模型层次结构,并将
GivenName
LastName
作为基本模型的属性,而不是使用
ViewBag

我们使用一个抽象控制器,并重写其
OnActionExecuting
方法,在调用实际操作方法之前执行代码。有了这个抽象控制器,您所要做的就是让任何其他控制器继承它以获得它的功能。我们还将此基本控制器用作定义其他辅助方法的位置,扩展它的其他控制器可以使用这些辅助方法,例如
GetUsernameForAuthenticatedUser()


此功能也可以在
属性
类中实现,该类允许您装饰控制器,而不是使用继承,但最终结果是相同的

使用
OnActionExecuting
的另一种替代方法是为模板的一部分提供自己的操作方法,该方法返回部分并调用
@Html.action()

@Mark…每个ActionResult是否在同一个控制器类中?它都在同一个控制器类中。至少现在是这样。以后可能会修改。两种方法都有不同的答案吗?@标记“是”,如果所有操作都在一个控制器中,只需覆盖该控制器的
OnActionExecuting
方法。否则,您需要考虑基本控制器或属性。以上将不起作用,因为这是通过ADFs调用的,并且没有用于拉动该数据的模型。如果不使用基本控制器,还有什么其他选项?我想用您已经从
ADFS
获得的数据创建并填充您的模型,但这不是重点。正如Jesse Webb所提到的,您可以在自己的控制器中执行
OnActionExecuting
方法中的代码,而这不需要继承基本控制器。因此,我可以在它自己的.cs文件中添加所需的代码,然后在my home controller中引用公共类HomeController:HelperController,它会自动获取代码并在该控制器中使用它吗?因此,在每个actionresult中,我实际上不需要上面提到的代码,因为它将在我从中继承的那个类中?@Mark Yes,这是正确的。此外,如果您想停止使用
ViewBag
,此解决方案将允许您为
GivenName
LastName
定义属性/方法,然后在为视图填充
ViewModel
时使用这些属性/方法。这种方法优于使用
ViewBag
的唯一原因是,这样您的值就可以是强类型的。请注意,我们如何为子控制器提供访问用户用户名的方法。。。也就是说,我们可以将其放入
视图模型中。
    public ActionResult Index()
    {
        ClaimsIdentity claimsIdentity = Thread.CurrentPrincipal.Identity as ClaimsIdentity;
        Claim claimGivenName = claimsIdentity.FindFirst("http://sts.msft.net/user/FirstName");
        Claim claimLastName = claimsIdentity.FindFirst("http://sts.msft.net/user/LastName");

        if (claimGivenName == null || claimLastName == null)
        {
            ViewBag.GivenName = "#FAIL";
        }
        else
        {
            ViewBag.GivenName = claimGivenName.Value;
            ViewBag.LastName = claimLastName.Value;
        }


        return View();
    }
public abstract class AbstractAuthenticationController : Controller
{
    private readonly IAuthenticationService _authService;

    protected AbstractAuthenticationController()
    {
        _authService = AuthenticationServiceFactory.Create();
    }

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        EnsureUserIsAuthenticated();
    }

    internal void EnsureUserIsAuthenticated()
    {
        if (!_authService.IsUserAuthenticated())
        {
            _authService.Login();
        }
    }

    protected string GetUsernameForAuthenticatedUser()
    {
        var identityName = System.Web.HttpContext.Current.User.Identity.Name;
        var username = _authService.GetUsername(identityName);
        if (username == null) throw new UsernameNotFoundException("No Username for " + identityName);
        return username;
    }
}