天道酬勤,学无止境

routedata

如何获取路由属性 dotnet core 3?(How can I get route attributes dotnet core 3?)

问题 我已将 dotnet core 2.2 升级到 3.preview 7。 所以在那之后,我无法获得自定义属性。 context.Resource在 2.2 版中是AuthorizationFilterContext类型,但在 3 版中是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

2021-10-25 03:34:08    分类:技术分享    c#   .net-core   authorization   .net-core-3.0   routedata

从控制器构造函数中的 URL 获取参数(get parameters from URL in controller constructor)

问题 我需要编写一些代码来在我的项目数据库中查找 ID。 用户耦合到一个项目,所有项目都与其他对象(例如会话)有很多连接。 现在我需要在运行任何操作之前检查,如果用户尝试访问会话,是否连接到会话连接到的同一个项目。 为此,我想在操作上使用 [属性]。 MVC:创建一个带参数的自定义 [AuthorizeAttribute]? 这个问题和答案让我开始了,但是我在使用控制器的构造函数来获取我的Project ID遇到了问题 目标是我可以在每个构造函数中编写一些代码,根据项目,我所有的对象控制器,找到项目 ID,并使其可访问(公开),因此我的[customauthorize]将有权访问此项目 ID检查用户是否有权访问。 我的问题: public class SessionController : Controller { NASDataContext _db = new NASDataContext(); public SessionController() { var test = RouteData; var ses = _db.Sessies.First(q=>q.Ses_ID==1); } 如何访问我的路由数据? RouteData为空, HttpContext为空, Request为空。 我需要url中的ID,它在routedata中... 回答1

2021-10-19 18:16:51    分类:技术分享    asp.net-mvc   constructor   routedata

How can I get route attributes dotnet core 3?

I have upgraded dotnet core 2.2 to 3.preview 7. So after that, I can't get custom attributes. context.Resource in version 2.2 was type of AuthorizationFilterContext, but in version 3 is type of Microsoft.AspNetCore.Http.Endpoint. Now I couldn't get attributes from 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

2021-09-03 20:18:43    分类:问答    c#   .net-core   authorization   .net-core-3.0   routedata

Asp.Net MVC Beta:以前的 RouteData 覆盖当前的 RouteData?(Asp.Net MVC Beta: Previous RouteData overrides current RouteData?)

问题 我有类似于以下方法的东西: public ActionResult Details(int id) { var viewData = new DetailsViewData { Booth = BoothRepository.Find(id), Category = ItemType.HotBuy }; return View(viewData); } 以及以下路线: routes.MapRoute("shows","shows/{controller}/{action}/{id}", new {id = 0}); 在 Beta 之前,当我使用 Preview 3 时一切正常。现在该方法将在我第一次执行操作时正确填充 id。 但是第二次控制器的ModelState包含上次使用的 id 值。 这会导致ActionInvoker在方法的参数而不是Route值中使用它。 因此,如果我在两个不同的实体上两次调用该操作,结果如下: www.mysite.com/shows/Booth/Details/1 => Details(1) www.mysite.com/shows/Booth/Details/2 => Details(1) //from ModelState["id"] 从我使用 Reflector 的快速扫描来看,它似乎首先将参数绑定到 ModelState,然后绑定到

2021-08-30 06:04:23    分类:技术分享    asp.net-mvc   routedata

Asp.Net MVC Beta: Previous RouteData overrides current RouteData?

I have something similar to the following method: public ActionResult Details(int id) { var viewData = new DetailsViewData { Booth = BoothRepository.Find(id), Category = ItemType.HotBuy }; return View(viewData); } and the following Route: routes.MapRoute("shows","shows/{controller}/{action}/{id}", new {id = 0}); Everything worked fine before the Beta, when I had Preview 3. Now the method will fill the id correctly the first time I execute the action. However the second time the controller's ModelState contains the last-use id value. This causes the ActionInvoker to use it in the method's

2021-07-02 01:57:51    分类:问答    asp.net-mvc   routedata

get parameters from URL in controller constructor

I need to write some code to find an ID in my database of a Project. Users are coupled to a project and all the projects have a lot of connections to other objects, such as Sessions. Now I need to check before running any Actions, if the user trying to access the Session, is connected to the same project as the session is connected to. For this i want to use an [Attribute] on the Actions. MVC: creating a custom [AuthorizeAttribute] which takes parameters? This question and answer got me started, but i'm having trouble using the constructor of the controller to get my Project ID the goal is

2021-06-25 14:50:41    分类:问答    asp.net-mvc   constructor   routedata

ASP.NET MVC 将列表传递给 RouteData QueryString(ASP.NET MVC Passing Lists to RouteData QueryString)

问题 将值列表作为 QueryString 传递的推荐方法是 www.site.com/search?value=1&value=2&value=3&value=4 ASP.NET 处理得很好: string value = QueryString.Get("value"); // returns "1,2,3,4" 但是我想不出将这些值传递到 RouteData 的方法。 显而易见的方法是添加 int[] value = {1,2,3,4}; 进入 RouteData 并让超级智能的 MVC 为我解决问题。 不幸的是,MVC 在将数组传递到 RouteData 时被转储,它基本上调用 .ToString() 将 value=int[] 添加到我的 QueryString。 我尝试将值添加到 RouteValueDictionary (但作为字典无法处理这个:) RouteValueDictionary dict = new RouteValueDictionary(); dict.Add("value","1"); dict.Add("value","2"); // Throws Exception (Keys must be unique) 我可以尝试传递这样的值: www.site.com/search?value=1,2,3,4 但这被编码为 URL www.site

2021-06-10 15:17:34    分类:技术分享    asp.net-mvc   query-string   routedata

如何从 .net MVC 中的 View 访问当前的 RouteData?(How do you access the current RouteData from View in .net MVC?)

问题 如果我想从View直接访问RouteData (例如当前操作或参数值),而不必将它作为ViewData一部分从控制器传入,我会使用什么类? 回答1 this.ViewContext.RouteData 应该做的伎俩。

2021-06-03 03:11:17    分类:技术分享    asp.net-mvc   routedata

ASP.NET MVC Passing Lists to RouteData QueryString

The recommended approach for passing lists of values as a QueryString is www.site.com/search?value=1&value=2&value=3&value=4 ASP.NET handles this well: string value = QueryString.Get("value"); // returns "1,2,3,4" But I can't figure out a way of passing these values by into RouteData. The obvious approach would be to add int[] value = {1,2,3,4}; into the RouteData and have super smart MVC sort things out for me. Unfortunately MVC is dump when it comes to passing arrays into RouteData, it basically calls .ToString() adding value=int[] to my QueryString. I tried adding the values to

2021-05-25 10:18:12    分类:问答    asp.net-mvc   query-string   routedata