小编wys*_*wyg的帖子

添加到htmlAttributes以获取自定义ActionLink帮助程序扩展

我正在尝试创建一个Html.ActionLink(...)HtmlHelper的简单自定义版本

我想为传入的htmlAttributes匿名对象附加一组额外的属性.

public static MvcHtmlString NoFollowActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
{
    var customAttributes = new RouteValueDictionary(htmlAttributes) {{"rel", "nofollow"}};
    var link = htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, customAttributes);
    return link;
}
Run Code Online (Sandbox Code Playgroud)

所以在我看来我会这样:

@Html.NoFollowActionLink("Link Text", "MyAction", "MyController")
Run Code Online (Sandbox Code Playgroud)

我希望能够呈现如下链接:

<a href="/MyController/MyAction" rel="nofollow">Link Text</a>
Run Code Online (Sandbox Code Playgroud)

但相反,我得到:

<a href="/MyController/MyAction" values="System.Collections.Generic.Dictionary`2+ValueCollection[System.String,System.Object]" keys="System.Collections.Generic.Dictionary`2+KeyCollection[System.String,System.Object]" count="1">Link Text</a>
Run Code Online (Sandbox Code Playgroud)

我已经尝试了将匿名类型转换为RouteValueDictionary的各种方法,添加到它然后将其传递到根ActionLink(...)方法或转换为Dictionary,或使用HtmlHelper.AnonymousObjectToHtmlAttributes并执行相同但似乎没有工作.

.net c# asp.net asp.net-mvc-4

8
推荐指数
1
解决办法
4858
查看次数

无法从context.Request.Form访问编码的表单数据

我正在我的请求管道中构建一些自定义中间件,如果发布的表单数据有效(挂钩到AspNetCore.Identity),它将生成JWT承载令牌.

基于Nate Barbettini的精彩教程:https://stormpath.com/blog/token-authentication-asp-net-core

我遇到了一个问题,在我的新中间件中可以访问HttpContext,抛出异常试图访问context.Reqeust.Form ["myfieldname"]

我使用postman发布表单数据:Content-Type:application/x-www-form-urlencoded

我可以调试我的中间件并检查context.Request.Form我看到这个:

Form = '((Microsoft.AspNetCore.Http.Internal.DefaultHttpRequest)((Microsoft.AspNetCore.Http.DefaultHttpContext)context).Request).Form' threw an exception of type 'System.MissingMethodException'
Run Code Online (Sandbox Code Playgroud)

如果我更改为Content-Type:multipart/form-data,我会看到以下错误:

Form = '((Microsoft.AspNetCore.Http.Internal.DefaultHttpRequest)((Microsoft.AspNetCore.Http.DefaultHttpContext)context).Request).Form' threw an exception of type 'System.IO.InvalidDataException'
Run Code Online (Sandbox Code Playgroud)

值得指出我正在使用RC2版本的Core:1.0.0-rc2-3002702 Api适用于所有Http动词,POST,PUT,GET,DELETE,我可以发布并获取数据没问题.

不明白为什么会发生这种情况或者我错过了一些基本的配置?从VS 2015模板开始,当前配置几乎是标准配置.

这可能是RC2的错误吗?

asp.net-web-api .net-core

5
推荐指数
0
解决办法
695
查看次数

标签 统计

.net ×1

.net-core ×1

asp.net ×1

asp.net-mvc-4 ×1

asp.net-web-api ×1

c# ×1