标签: controller

结构图控制器工厂和MVC中的空控制器实例

我还在尝试使用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)

可能有什么不对?我是否需要注册每个控制器?谢谢.

structuremap model-view-controller null controller

10
推荐指数
2
解决办法
4559
查看次数

MVC导航到不同的控制器视图

我无法从另一个控制器操作导航到另一个控制器视图.例如,返回视图("编辑","配置文件",配置文件);

我正在尝试从帐户控制器导航到配置文件控制器中的编辑视图.

有什么建议?

谢谢,-Mike

asp.net-mvc controller view

10
推荐指数
1
解决办法
1万
查看次数

有没有办法让Codeigniter控制器返回图像?

我想知道控制器是否有办法,而不是返回一个字符串,或一个视图,返回一个图像(无论是JPG,PNG等).例如,我不想以$ this-> load-> view('folder/special_view.php)结尾,而是想做像$ this-> load-> image('images/gorilla.png')这样的事情.并且如果我的用户要去那个控制器他们会看到一个图像,好像他们去了正常的.png或jpeg.我可以设置标头,以便它需要不同的MIME吗?这个示例代码太棒了.

我需要永远解释为什么我需要这个,但它涉及将预制的CMS带入codeigniter,并让它需要真实的事情.非常感谢!

mime controller codeigniter image

10
推荐指数
3
解决办法
2万
查看次数

在ASP.NET MVC中的控制器之外创建ViewResults

我的一些控制器操作具有一组标准的故障处理行为.一般来说,我想:

  • 根据路径数据(ID等)加载对象
    • 如果Route Data未指向有效对象(例如:通过URL hacking),则通知用户该问题并返回HTTP 404 Not Found
  • 验证当前用户是否具有该对象的适当权限
    • 如果用户没有权限,请通知用户该问题并返回HTTP 403 Forbidden
  • 如果上述内容成功,则使用特定于操作的对象执行某些操作(即:在视图中呈现它).

这些步骤是如此标准化,我希望有可重用的代码来实现该行为.

我目前的攻击计划是使用辅助方法来执行以下操作:

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的最佳方法是什么?

asp.net-mvc controller

10
推荐指数
2
解决办法
9798
查看次数

CakePHP Auth如何允许特定的控制器和操作

我有一个"帖子"和一个"用户"控制器.我使用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

authentication controller cakephp

10
推荐指数
1
解决办法
3万
查看次数

HttpApplicationState在MVC控制器中不可用

我正在使用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

c# controller global-variables global-asax asp.net-mvc-2

10
推荐指数
1
解决办法
7094
查看次数

如何使用RSpec测试CanCan失败授权的响应代码?

我正在开发一个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)

controller rspec cancan rspec-rails

10
推荐指数
1
解决办法
1万
查看次数

Rails设计会话控制器

在设计中,许多教授如何完成某些事情的页面需要编辑会话控制器.我使用这个https://github.com/fortuity/rails3-subdomain-devise/wiki/Tutorial-(Walkthrough)设置了设计.它没有重新制作会话控制器.我怎么做一个.(如果真的很容易对不起,请给我几个简单的步骤)

session controller ruby-on-rails devise

10
推荐指数
1
解决办法
1万
查看次数

如何编写确保发送电子邮件的Rspec控制器测试?

目前在我的控制器规范中我有:

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

10
推荐指数
1
解决办法
5424
查看次数

如何在CodeIgniter中的库中使用会话?

我想通过在控制器的构造函数中使用我的库来检查用户是否登录了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中的库中使用会话?

php controller codeigniter

10
推荐指数
1
解决办法
2万
查看次数