我还在尝试使用StructureMap来解决问题,而我遇到的一个问题是我的Controller Factory类在传递空控制器类型时会爆炸.这仅在应用程序第一次构建时发生,之后每个后续构建都可以正常工作.即使我关闭Visual Studio并重新打开项目(我没有在IIS中运行它).这几乎就像有某种缓存一样.这是控制器类的样子:
public class IocControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(Type controllerType)
{
try
{
return (Controller)ObjectFactory.GetInstance(controllerType);
}
catch (StructureMapException)
{
System.Diagnostics.Debug.WriteLine(ObjectFactory.WhatDoIHave());
throw;
}
}
}
Run Code Online (Sandbox Code Playgroud)
可能有什么不对?我是否需要注册每个控制器?谢谢.
我无法从另一个控制器操作导航到另一个控制器视图.例如,返回视图("编辑","配置文件",配置文件);
我正在尝试从帐户控制器导航到配置文件控制器中的编辑视图.
有什么建议?
谢谢,-Mike
我想知道控制器是否有办法,而不是返回一个字符串,或一个视图,返回一个图像(无论是JPG,PNG等).例如,我不想以$ this-> load-> view('folder/special_view.php)结尾,而是想做像$ this-> load-> image('images/gorilla.png')这样的事情.并且如果我的用户要去那个控制器他们会看到一个图像,好像他们去了正常的.png或jpeg.我可以设置标头,以便它需要不同的MIME吗?这个示例代码太棒了.
我需要永远解释为什么我需要这个,但它涉及将预制的CMS带入codeigniter,并让它需要真实的事情.非常感谢!
我的一些控制器操作具有一组标准的故障处理行为.一般来说,我想:
这些步骤是如此标准化,我希望有可重用的代码来实现该行为.
我目前的攻击计划是使用辅助方法来执行以下操作:
public static ActionResult HandleMyObject(this Controller controller,
Func<MyObject,ActionResult> onSuccess) {
var myObject = MyObject.LoadFrom(controller.RouteData).
if ( myObject == null ) return NotFound(controller);
if ( myObject.IsNotAllowed(controller.User)) return NotAllowed(controller);
return onSuccess(myObject);
}
# NotAllowed() is pretty much the same as this
public static NotFound(Controller controller){
controller.HttpContext.Response.StatusCode = 404
# NotFound.aspx is a shared view.
ViewResult result = controller.View("NotFound");
return result;
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是Controller.View()是一个受保护的方法,因此无法从帮助程序访问.我已经看过明确地创建一个新的ViewResult实例,但是有足够的属性来设置我在不知道陷阱的情况下这么做是很谨慎.
从特定控制器外部创建ViewResult的最佳方法是什么?
我有一个"帖子"和一个"用户"控制器.我使用Auth组件,我希望所有用户都可以访问"Post.index",但只有登录用户才能访问"User.index".
在我的app_controller.php中,我有这个
$this->Auth->allow('signup', 'confirm', 'index');
Run Code Online (Sandbox Code Playgroud)
但是所有用户都可以访问post.index和user.index.如何在allow-method中指定Controller?
这不适合我:
$this->Auth->allow('signup', 'confirm', 'Post.index');
Run Code Online (Sandbox Code Playgroud)
更新 我从app_controller.php中删除了'index',而是将其设置在post控制器的beforeFilter方法中:
function beforeFilter()
{
parent::beforeFilter();
$this->Auth->allow('index');
}
Run Code Online (Sandbox Code Playgroud)
我还在app_controller中设置了一个变量"loggedIn",而没有调用"parent :: beforeFilter();" 我收到了一个"未定义的变量"通知.
thx sibidiba
我正在使用MVC2和VS2010开发一个网站,需要使用Application State全局值.我可以设置一个像'Application ["hits"] = 0;'的值 在Global.asax中但是当尝试在MVC控制器中使用相同时,总是会出现以下错误:
"应用程序"名称在当前上下文中不存在
我也尝试在Global.asax中使用以定义全局变量,但它会触发以下错误:
命名空间不能直接包含字段或方法等成员
我正在寻找一种方法来定义我的MVC2 Web应用程序的所有控制器中可用的全局应用程序状态值.我省略了什么吗?我的控制器看起来像这样:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCApplication.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
Application["hits"] += 1;
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我感谢任何解决方案和/或建议.
谢谢Mehrdad
我正在开发一个rails项目,我使用CanCan来授权我的资源.当用户未登录并尝试提交"谈话"(通过ajax表单提交)时,CanCan会正确引发401 {"status":"error","message":"You must be logged in to do that!"}作为响应(我已在浏览器中使用firebug验证了这一点).但是,在我的测试中得到302响应代码而不是401:
class TalksController < ApplicationController
authorize_resource
def create
@talk = current_user.talks.build(params[:talk])
respond_to do |format|
if @talk.save
response = { :redirect => talk_path(@talk) }
format.html { redirect_to @talk, notice: 'Talk was successfully created.' }
format.json { render json: response, status: :created, }
else
format.html { render action: "new" }
format.json { render json: @talk.errors, status: :unprocessable_entity }
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
talks_controller_spec.rb:
describe TalksController do
describe "POST create" do
context "when not …Run Code Online (Sandbox Code Playgroud) 在设计中,许多教授如何完成某些事情的页面需要编辑会话控制器.我使用这个https://github.com/fortuity/rails3-subdomain-devise/wiki/Tutorial-(Walkthrough)设置了设计.它没有重新制作会话控制器.我怎么做一个.(如果真的很容易对不起,请给我几个简单的步骤)
目前在我的控制器规范中我有:
require 'spec_helper'
describe CustomerTicketsController do
login_user
describe "POST /create (#create)" do
# include EmailSpec::Helpers
# include EmailSpec::Matchers
it "should deliver the sales alert email" do
# expect
customer_ticket_attributes = FactoryGirl.attributes_for(:customer_ticket)
customer_mailer = mock(CustomerMailer)
customer_mailer.should_receive(:deliver).
with(CustomerTicket.new(customer_ticket_attributes))
# when
post :create, :customer_ticket => customer_ticket_attributes
end
end
end
Run Code Online (Sandbox Code Playgroud)
在我的控制器中我有:
# POST /customer_tickets
# POST /customer_tickets.xml
def create
respond_to do |format|
if @customer_ticket.save
CustomerMailer.sales_alert(@customer_ticket).deliver
format.html { redirect_to @customer_ticket, notice: 'Customer ticket was successfully created.' }
format.xml { render xml: @customer_ticket, status: :created, location: …Run Code Online (Sandbox Code Playgroud) controller ruby-on-rails actionmailer rspec-rails ruby-on-rails-3
我想通过在控制器的构造函数中使用我的库来检查用户是否登录了CodeIgniter.
这是我的图书馆:
class Administrator_libs {
public function validate_authen(){
if( $this->session->userdata('user_authen') ){
redirect(base_url().'admin/login/');
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
class Administrator extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->library('administrator_libs');
$this->administrator_libs->validate_authen();
$this->load->model('mod_menu');
}
}
Run Code Online (Sandbox Code Playgroud)
但我收到此错误消息:
Undefined property: Administrator_libs::$session
Run Code Online (Sandbox Code Playgroud)
如何在CodeIgniter中的库中使用会话?
controller ×10
asp.net-mvc ×2
codeigniter ×2
rspec-rails ×2
actionmailer ×1
c# ×1
cakephp ×1
cancan ×1
devise ×1
global-asax ×1
image ×1
mime ×1
null ×1
php ×1
rspec ×1
session ×1
structuremap ×1
view ×1