我们知道在幕后,ASP.NET MVC框架将使用反射来确定哪些控制器/操作可以执行,基于哪些类派生自System.Web.Mvc.Controller哪些类,哪些方法返回一个ActionResult对象.
对我的问题 - 是否可以从我的MVC应用程序中访问此控制器/操作列表?
(我可以通过在当前程序集上使用反射来自己完成,但如果列表已经由ASP.NET MVC构建,我宁愿重新使用这些工作,而不是自己重新发明轮子.)
我从Model(带数据的数组)中获取数据,我需要以特定格式显示.我需要迭代数组,格式化数据然后显示它.我应该在哪里格式化数据?在模型,控制器或视图中?谢谢.
这里的帖子中有人评论说在使用MVC时不应该使用HttpContext.Current,而是应该使用ControllerBase.ControllerContext.在某些方面,这是有道理的,但在其他方面却没有.
例如,ControllerContext是一个实例变量,所以在我想引用的所有地方,比如我的Session变量,我需要有一个对Controller的引用?为什么我们"不应该"在MVC中使用HttpContext.Current,当你还可以的时候?是否有一个"适当的"MVC"方式"来获取我的Session对象而无需引用Controller?
我知道测试方面,它更好的原因在许多其他地方说明,但我正在研究一个管理Session变量和引用HttpContext.Current的项目,我想知道是否有更好的方法来获取Session对象没有传递对控制器的引用.
我对RoR很新,我正在寻找一种方法来获取给定控制器,动作和参数的路径.
类似于url_for()但没有域和协议的东西.
让我们说:
params = {"controller"=>"controller", "action"=>"edit", "project_id"=>"1"}
Run Code Online (Sandbox Code Playgroud)
我需要得到 :
route = "/controller/edit/1"
Run Code Online (Sandbox Code Playgroud)
如果我不必手动构建路由并且不需要拆分url_for()的结果,那将是最好的.
RoR本身是否支持这样的功能?这可能是一个简单的问题,但我找不到答案.
这是一个示例动作过滤器.我们知道当我们编写一个动作过滤器时,我们需要用这样的属性来装饰控制器,以便将它用于任何控制器.
我想知道是否有任何方法可以编写一个动作过滤器,它将适用于所有控制器,我不需要使用动作过滤器属性来装饰所有控制器.有任何想法吗?
[LogActionFilter]
public class HomeController : Controller
{}
public class LogActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
Log("OnActionExecuting", filterContext.RouteData);
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
Log("OnActionExecuted", filterContext.RouteData);
}
private void Log(string methodName, RouteData routeData)
{
var controllerName = routeData.Values["controller"];
var actionName = routeData.Values["action"];
var message = String.Format("{0} controller:{1} action:{2}", methodName, controllerName, actionName);
Debug.WriteLine(message, "Action Filter Log");
}
}
Run Code Online (Sandbox Code Playgroud) 嗨,我的Sample中有一个简单的RestController:
@RestController
public class PersonController {
@RequestMapping(name = "/getName", method = GET)
public String getName() {
return "MyName";
}
@RequestMapping(name = "/getNumber", method = GET)
public Double getNumber(){
return new Double(0.0);
}
}
Run Code Online (Sandbox Code Playgroud)
我有SampleController用于启动SpringBoot:
@SpringBootApplication
@Controller
public class SampleController {
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行SampleCotroller时,会发生以下异常:
Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'personController' bean method
public java.lang.Double com.web.communication.PersonController.getNumber()
to {[],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'personController' bean method
public java.lang.String com.web.communication.PersonController.getName() mapped.
Run Code Online (Sandbox Code Playgroud)
问题出在哪里?一个RestController中不能有更多的RequestMappings? …
我知道在 asp.net-core 2.2 中,我可以按如下方式获取操作名称:
ControllerContext.ActionDescriptor.ActionName
Run Code Online (Sandbox Code Playgroud)
但我正在寻找一种方法来获取 asp.net-core 2.2 中当前请求/页面的控制器名称
我将不胜感激任何指南。
谢谢
我使用 Symfony 5.1.2 创建了一个新项目,然后尝试使用
symfony serve. 当我尝试访问时localhost:8000,我在终端中收到此警告:[Web Server/PHP ] Jun 22 13:47:55 |WARN | SERVER GET (404) / ip="::1"。
此外,我的页面是 symfony 的默认页面,显示“#Page not found”。
我检查了现有的路线,我有default ANY ANY ANY /。
我还检查了DefaultController,但一切看起来都很好,因为它是自动生成的控制器......
我不知道还要检查什么,如何让这个默认页面正常工作?
如何在 ASP.NET Core 中使控制器作用域或单例而不是瞬态?
我现在默认情况下,控制器注册到具有瞬态生命周期的默认 DI 容器。
如果我想用不同的生命周期注册它们,我该怎么做?
我想知道这仅仅是出于教育目的,以便更好地了解 DI 容器的控制器类型管理。
我目前正在按照升级指南将 Rails 5.2 应用程序升级到 Rails 6.0。
一切似乎都工作得很好,直到我的用户类之一(标签或艺术家)与链接模型交互时遇到错误。
当我尝试以艺术家或厂牌身份注册时,当我需要定义指向用户社交媒体或网站的链接时,我收到以下错误:
Completed 500 Internal Server Error in 139ms (ActiveRecord: 7.3ms | Allocations: 14518)
NoMethodError - undefined method `extensions' for #<Hash:0x000000000bdd4ec8>
Did you mean? extend:
app/controllers/label/personal_informations_controller.rb:8:in `edit'
Run Code Online (Sandbox Code Playgroud)
这是我的个人信息控制器:(错误位于第 8 行,我将对其进行标记,以便清楚地显示)
class Label::PersonalInformationsController < LabelController
layout "signup"
skip_before_action :ensure_approved
def edit
prepare_country_options
@label = current_label
@label.links.build(links_attributes) #***ERROR OCCURS HERE***#
end
def update
@label = current_label
if @label.update_attributes(label_params)
redirect_to edit_label_genres_path
else
update_error
end
end
private
def update_error
prepare_country_options
@label.links.build(links_attributes)
render :edit
end
def label_params
params.require(:label).permit(:logo, …Run Code Online (Sandbox Code Playgroud) controller ×10
asp.net-mvc ×3
action ×2
httpcontext ×2
model ×2
php ×2
asp.net-core ×1
attributes ×1
c# ×1
reflection ×1
rest ×1
routes ×1
ruby ×1
spring ×1
spring-boot ×1
symfony ×1
transient ×1
url ×1
view ×1