如果我想直接访问RouteData(例如当前动作或参数值)View,而不是必须从控制器传入它作为一部分ViewData,我将使用哪个类?
我很惊讶地发现没有任何文章能够回答这个问题.我几乎没有相关的问题RouteData.Values[""].
我看到了这段代码:
public ActionResult Index()
{
ViewBag.Message = string.Format("{0}---{1}--{2}",
RouteData.Values["Controller"],
RouteData.Values["action"],
RouteData.Values["id"]);
return View();
}
Run Code Online (Sandbox Code Playgroud)
这里基本上是读取可能听起来像控制器的"元数据"的值.或者View也可以传递给Controller?
在常规课程中,我需要阅读以下内容HttpContext:
控制器和操作名称
动作的属性(我可以通过HttpActionContext.ActionDescriptor.GetCustomAttributes<type>() 但这里我没有HttpActionContext- 我只有HttpContext)
阅读论证(例如actionContext.ActionArguments["paramName"],但同样 - 我只有一个HttpContext)
它不是动作过滤器,也不是控制器类。但是,我可以访问HttpContext.
httpcontext routedata asp.net-web-api actioncontext asp.net-web-api2
我已将 dotnet core 2.2 升级到 3.preview 7。
因此之后,我无法获取自定义属性。
context.Resource在版本 2.2 中是类型 of AuthorizationFilterContext,但在版本 3 中是类型 of Microsoft.AspNetCore.Http.Endpoint。
现在我无法从端点获取属性。
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace Gamma.Core.Security
{
public abstract class AttributeAuthorizationHandler<TRequirement, TAttribute>
: AuthorizationHandler<TRequirement> where TRequirement
: IAuthorizationRequirement where TAttribute : Attribute
{
Microsoft.AspNetCore.Http.IHttpContextAccessor _httpContextAccessor = null;
public AttributeAuthorizationHandler(Microsoft.AspNetCore.Http.IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, TRequirement requirement)
{
var attributes = new …Run Code Online (Sandbox Code Playgroud) 我正在运行一个 MVC 项目并利用 Json,我的代码在 Visual Studio 中运行时可以正常运行,然后我在 IIS 上有一个指向同一文件夹的站点,当从 IIS 站点执行 URL 时,我的代码没有执行方式与在 Visual Studio 中相同。
在我的代码中,我有:
return JsonConvert.SerializeObject(objectToSerialize);
Run Code Online (Sandbox Code Playgroud)
当我发送 RouteData.Values 时,它会产生以下错误:
Error getting value from 'CompiledAssembly' on 'System.CodeDom.Compiler.CompilerResults'.
at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeISerializable(JsonWriter writer, ISerializable value, JsonISerializableContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType) …Run Code Online (Sandbox Code Playgroud) 我试图使用Web服务从URL中提取id号,以便它可以用作select语句中where子句的参数,该语句根据记录的id号从数据库生成数据.然后将该数据传递回页面以填充jQuery模式弹出窗口中的元素.
使用静态ID号(ex :),一切正常string postid = "120",但我不知道如何从URL获取id号.我在.Net 4中使用路由,并且在页面中访问路由的方法在Web服务中不起作用.在页面中我只是做类似的东西,var id = RouteData.Values["id"];并获得id,但当我在Web服务中做到这一点时,我收到一个错误:
CS0120:非静态字段,方法或属性需要对象引用
'System.Web.Routing.RouteData.Values.get'
摘要:
我从一个详细信息页面访问了Web服务,我想要获取RouteData请求的页面.我想在页面上尽可能轻松地使用RouteData.Values它,就像现在过时一样容易Request.Querystring.
现在我更加困惑,因为虽然我可以轻松地为Web服务添加新路由,但我不知道因为webservice.asmx/webmethod语法而使用jQuery Ajax.
现在我的jQuery Ajax中有URL:"../webservices /googlemaps.asmx/GetGoogleMap",但这不是真正的URL.它只存在于某个地方的jQuery中,并且使用JavaScript调用服务的方式也不是真正的URL,webservice.webmethod()在这种情况下它就是googlemaps.GetGoogleMap().
我会尝试注册一个路由webservices/googlemaps.asmx/GetGoogleMap/postid,但我怀疑它会起作用,因为GetGoogleMap它不是一个目录或一个querystring.
routedata ×6
c# ×3
asp.net-mvc ×2
.net-core ×1
asp.net ×1
httpcontext ×1
json ×1
query-string ×1
routing ×1
web-services ×1