Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.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# 如何在CS文件中创建友好的URL_C#_Asp.net Mvc - Fatal编程技术网

C# 如何在CS文件中创建友好的URL

C# 如何在CS文件中创建友好的URL,c#,asp.net-mvc,C#,Asp.net Mvc,我知道如何使用aspx文件中的html.actionlink创建url。但是,如果我想在代码隐藏文件中创建相同的url,我该怎么做呢?看看Scott Mitchell的这篇文章 (既然你说“html.actionlink”是UrlHelper类的一个实例,我假设你所处的环境中没有访问UrlHelper类实例的权限)MVC中视图背后的代码被删除了,因为它似乎并不真正符合MVC范式。也许你应该考虑一下。要做到这一点,扩展像Html.ActionLink()这样的现有操作很容易(而且非常有趣) 这个

我知道如何使用aspx文件中的html.actionlink创建url。但是,如果我想在代码隐藏文件中创建相同的url,我该怎么做呢?

看看Scott Mitchell的这篇文章


(既然你说“html.actionlink”是UrlHelper类的一个实例,我假设你所处的环境中没有访问UrlHelper类实例的权限)

MVC中视图背后的代码被删除了,因为它似乎并不真正符合MVC范式。也许你应该考虑一下。要做到这一点,扩展像
Html.ActionLink()
这样的现有操作很容易(而且非常有趣)

这个例子展示了我如何创建一个助手来调整我的登录/注销链接。一些ppl可能会争论这对助手来说是否是一个好的用途,但对我来说是有效的:

    /// <summary>
    /// For the global MasterPage's footer
    /// </summary>
    /// <returns></returns>
    public static string FooterEditLink(this HtmlHelper helper,
        System.Security.Principal.IIdentity user, string loginText, string logoutText)
    {
        if (user.IsAuthenticated)
            return System.Web.Mvc.Html.LinkExtensions.ActionLink(helper, logoutText, "Logout", "Account",
                new { returnurl = helper.ViewContext.HttpContext.Request.Url.AbsolutePath }, null);
        else
            return System.Web.Mvc.Html.LinkExtensions.ActionLink(helper, loginText, "Login", "Account",
                new { returnurl = helper.ViewContext.HttpContext.Request.Url.AbsolutePath }, null);
    }
//
///用于全局母版页的页脚
/// 
/// 
公共静态字符串FooterEditLink(此HtmlHelper帮助程序,
System.Security.Principal.IIdentity用户、字符串登录文本、字符串登录文本)
{
if(user.IsAuthenticated)
return System.Web.Mvc.Html.LinkExtensions.ActionLink(helper,logoutText,“Logout”,“Account”,
新的{returnurl=helper.ViewContext.HttpContext.Request.Url.AbsolutePath},null);
其他的
return System.Web.Mvc.Html.LinkExtensions.ActionLink(helper,loginText,“Login”,“Account”,
新的{returnurl=helper.ViewContext.HttpContext.Request.Url.AbsolutePath},null);
}
…这就是我在视图中使用它的方式(确切地说是局部视图):


<% =Html.FooterEditLink(HttpContext.Current.User.Identity, "Edit", "Logout (" + HttpContext.Current.User.Identity.Name + ")")%>