在Laravel 4中,我的控制器使用Blade布局:
class PagesController extends BaseController {
protected $layout = 'layouts.master';
}
Run Code Online (Sandbox Code Playgroud)
主布局输出变量标题,然后显示视图:
...
<title>{{ $title }}</title>
...
@yield('content')
....
Run Code Online (Sandbox Code Playgroud)
但是,在我的控制器中,我似乎只能将变量传递给子视图,而不是布局.例如,一个动作可能是:
public function index()
{
$this->layout->content = View::make('pages/index', array('title' => 'Home page'));
}
Run Code Online (Sandbox Code Playgroud)
这只会将$title变量传递给视图的内容部分.如何将变量提供给整个视图,或者至少是主布局?
我正在尝试在web api自托管主机中获取用户代理,我要么做错了,要么web api本身正在改变用户代理字符串.
我尝试使用几种方法来获取字符串,它们都返回相同的结果,而不是例外的"Mozilla/5.0(Windows NT 6.2; WOW64)AppleWebKit/537.31(KHTML,像Gecko)Chrome/26.0.1410.28 Safari /537.31",我只得到"Mozilla/5.0".
我试过了:
var header = request.Headers.SingleOrDefault(h => h.Key == "User-Agent").Value.First();
var header = request.Headers.UserAgent.SingleOrDefault().Product.ToString();
var header = request.Headers.GetValues("User-Agent").FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
我做错了,它是自我主持人所以我没有上下文可以使用.
我开始编写单元测试(MS测试,Resharper作为测试运行器).当我设置LogicalThreadContext(见下文)时,我的测试用例被"中止".谁知道为什么?这与单元测试在不同的线程上有关吗?我该如何解决这个问题?
[TestClass]
public class ContextInfoTest
{
private ILog _log;
[TestInitialize]
public void TestInitialize()
{
// logging configured in assembly.info
_log = LogManager.GetLogger(this.GetType());
}
[TestMethod]
public void FigureOutWhyAborting()
{
string input = "blah";
LogicalThreadContext.Properties["mypropertyname"] = input;
string output = LogicalThreadContext.Properties["mypropertyname"] as string;
Assert.AreEqual(input, output);
}
[TestMethod]
public void ThisWorks()
{
string input = "blah";
CallContext.LogicalSetData("mypropertyname", input);
string output = CallContext.LogicalGetData("mypropertyname") as string;
Assert.AreEqual(input, output);
}
Run Code Online (Sandbox Code Playgroud)
奇怪的是,如果我要调试并逐步执行代码,Assert.AreEqual会被调用并传递,因此在该行代码之后发生了一些事情...这就是为什么我认为它可能与某些事情有关测试线程等
谢谢!
更新:所以我在MSTest中运行了这个测试并得到了这个例外(Resharper没有显示它)
单元测试适配器抛出异常:成员'log4net.Util.PropertiesDictionary,log4net,Version = 1.2.13.0,Culture = neutral,PublicKeyToken = 669e0ddf0bb1aa2a'的类型未解析..
我在VS2013,.Net 4.5上使用log4net v1.2.13.
这个链接似乎暗示它是一个引用的程序集问题,但没有解决方案.非常欢迎任何其他想法,GAC'log4net不是一个选项. https://issues.apache.org/jira/browse/LOG4NET-398
我开始研究Web Api,只想创建一个简单的基本身份验证.我想知道怎么做?
我尝试使用给定的MSDN链接,但在MSDN上没有给出逐步教程. http://www.asp.net/web-api/overview/security/basic-authentication
我有一个BasicAuthenticationAttribute检查请求中的Authorization标头但是尽管它存在,它仍然认为Authorization标头为null:
public class BasicAuthenticationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.Request.Headers.Authorization == null)
{
actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
}
...
Run Code Online (Sandbox Code Playgroud)
如果我检查actionContext.Request.Headers我可以看到Authorization列出:
{Connection: Keep-Alive
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-gb
Authorization: REDACTED_BUT_PRESENT==
Host: localhost:44300
Referer: https://localhost:44300/
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3; .NET4.0E)
}
Run Code Online (Sandbox Code Playgroud)
更新
我刚刚检查了完整的请求标头,它们看起来像这样......我可以看到第一部分中的Authorization标头,但第二部分中的Authorization标头显然为null.
request.Headers
{Connection: Keep-Alive
Accept: …Run Code Online (Sandbox Code Playgroud) 我有一个post动作接收Person类型的FromBody参数.在HelpPage中,我获得有关Person paramater的信息.是否可以在Person中列出有关属性的信息,并使用XML文档文件中的文档来获取每个属性的描述?
public class PersonController : ApiController
{
/// <summary>
/// Add a person
/// </summary>
/// <param name="person">Person to add</param>
/// <returns></returns>
[HttpPost]
public HttpResponseMessage Add([FromBody] Person person)
{
// ...
return Request.CreateResponse(HttpStatusCode.Created);
}
}
/// <summary>
/// A person
/// </summary>
public class Person
{
/// <summary>
/// The name of the person
/// </summary>
public String Name { get; set; }
/// <summary>
/// The age of the person
/// </summary>
public Int32 Age { get; set; …Run Code Online (Sandbox Code Playgroud) 给定具有这些数据注释的模型:
public class Example
{
[Required]
[Display(Name = "Activity response")]
public string ActivityResponse { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我希望模型状态错误消息是"活动响应字段是必需的".相反,它是"ActivityResponse字段是必需的".
以下简单程序将找到用户输入的字符串中的最后一个字母,然后删除该点之后的所有内容.所以,如果一个人string....在g应该被删除后输入一切.我有以下作为一个小程序:
class Program
{
static void Main(string[] args)
{
Console.Write("Enter in the value of the string: ");
List<char> charList = Console.ReadLine().Trim().ToList();
int x = charList.LastIndexOf(charList.Last(char.IsLetter)) ;
Console.WriteLine("this is the last letter {0}", x);
Console.WriteLine("This is the length of the string {0}", charList.Count);
Console.WriteLine("We should have the last {0} characters removed", charList.Count - x);
for (int i = x; i < charList.Count; i++)
{
charList.Remove(charList[i]);
}
foreach (char c in charList)
{
Console.Write(c);
}
Console.ReadLine();
}
} …Run Code Online (Sandbox Code Playgroud) 我认为unionLaravel 4和Laravel 4.1之间有一些变化.我有2个型号.
$photos = DB::table('photos')->select('id', 'name', 'created_at');
$videos = DB::table('videos')->select('id', 'name', 'created_at');
Run Code Online (Sandbox Code Playgroud)
我想结合2个查询并命令2个查询created_at字段.
$photos = $photos->orderBy('created_at', 'desc');
$combined = $photos->union($videos);
Run Code Online (Sandbox Code Playgroud)
使用Laravel 4,它给了我这个查询:
select `id`, `name`, `created_at` from `videos`
union
select `id`, `name`, `created_at` from `photos`
order by `created_at` desc
Run Code Online (Sandbox Code Playgroud)
这样就行了,它将两个查询的结果排序在一起.在Laravel 4.1中它给了我这个查询:
(select `id`, `name`, `created_at` from `videos`)
union
(select `id`, `name`, `created_at` from `photos` order by `created_at` desc)
Run Code Online (Sandbox Code Playgroud)
这会产生一个视频列表,然后是一个有序的照片列表.我需要有一个列表,其中组合的查询被排序.我希望Laravel给我这个问题:
(select `id`, `name`, `created_at` from `videos`)
union
(select `id`, `name`, `created_at` from `photos`)
order by `created_at` …Run Code Online (Sandbox Code Playgroud) Laravel 4 中有一个dateTime()函数可以帮助创建这些列。但在Schema\TableLaravel 3 中,我只看到一个date()函数,它是:
public function date($name)
{
return $this->column(__FUNCTION__, compact('name'));
}
Run Code Online (Sandbox Code Playgroud)
看起来它只是创建了一个 DATE 列。不过,我也看到了这个神奇的功能:
public function __call($method, $parameters)
{
if (isset(static::$macros[$method]))
{
array_unshift($parameters, $this);
return call_user_func_array(static::$macros[$method], $parameters);
}
throw new \Exception("Method [$method] does not exist.");
}
Run Code Online (Sandbox Code Playgroud)
有没有办法使用此函数创建 DATETIME 列?我该怎么办?在文档中找不到此信息。
laravel ×3
laravel-4 ×2
blade ×1
c# ×1
eloquent ×1
laravel-3 ×1
log4net ×1
php ×1
self-hosting ×1
unit-testing ×1
validation ×1