示例代码:在Visual Studio的MVC 5项目中,我设置了以下控制器和视图:
调节器
TestController.cs
public class TestController : BaseController
{
public ActionResult Index()
{
return View("Index");
}
public PartialViewResult MyPartialView1()
{
return PartialView("PartialView1");
}
public PartialViewResult MyPartialView2()
{
return PartialView("PartialView2");
}
}
Run Code Online (Sandbox Code Playgroud)
查看
Index.cshtml
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<script src="@Url.Content("~/Scripts/jquery-2.1.1.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")"></script>
</head>
<body>
<h1>Testing Unobtrusive AJAX</h1>
@Ajax.ActionLink("Partial 1", "MyPartialView1", "Test",
new AjaxOptions() { UpdateTargetId = "contentShouldLoadHere", InsertionMode = InsertionMode.Replace })
|
@Ajax.ActionLink("Partial 2", "MyPartialView2", "Test",
new AjaxOptions() { UpdateTargetId = "contentShouldLoadHere", InsertionMode = …Run Code Online (Sandbox Code Playgroud) 我有一个 .NET Core 项目,我正在将它构建到 docker 映像中。Dockerfile 看起来像这样:
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-alpine AS restore
WORKDIR /tmp/build
COPY ./*.sln .
COPY ./*/*.csproj ./
# Put project files back into their own project directories
RUN for file in $(ls *.csproj); do mkdir -p ${file%.*}/ && mv $file ${file%.*}/; done
RUN dotnet restore
# Copy in the source files
FROM restore AS build
WORKDIR /tmp/build
COPY . .
RUN dotnet publish -o output MyApp/MyApp.csproj
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-alpine AS runtime
WORKDIR /app
COPY --from=build /tmp/build/output .
ENTRYPOINT [ …Run Code Online (Sandbox Code Playgroud) 我在Web API中编写RESTful API,我不确定如何有效地处理错误.我希望API返回JSON,它需要每次都包含完全相同的格式 - 即使出现错误.以下是一些成功和失败的响应可能是什么样子的例子.
成功:
{
Status: 0,
Message: "Success",
Data: {...}
}
Run Code Online (Sandbox Code Playgroud)
错误:
{
Status: 1,
Message: "An error occurred!",
Data: null
}
Run Code Online (Sandbox Code Playgroud)
如果有异常 - 任何异常,我想返回一个像第二个那样形成的响应.什么是万无一失的方法,以便没有任何例外处理未处理?
我正在开发一个RESTful API,我在为API提供输入的过程中遇到了一些麻烦.
假设我有一个可以像这样获取的"Person"资源:api/person/{id}并返回如下对象:
public class Person
{
public int Id { get; set; }
public string Surname { get; set; }
public string GivenName { get; set; }
public DateTime DateOfBirth { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如果我想更新那个人,API应该期待一个完整的Person实例,还是可以使用单独的DTO?
比如说,DateOfBirth不能改变,接受它作为输入是否被认为是RESTful:
public class UpdatePersonDto
{
public string Surname { get; set; }
public string GivenName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这意味着我将不得不为此端点api/person/{id}恢复Person使用时GET,而作为输入接收UpdatePersonDto使用时PUT.这对我来说听起来不对,但我不确定我是不是只是偏执狂.
所以我想我的问题总结为:接受给定资源端点上的数据结构是否适合与端点返回的数据结构不同?
非常基本的问题,我正在尝试为Laravel中的正则表达式验证规则自定义错误消息.特定的规则是密码,并要求密码有6-20个字符,至少一个数字和一个大写和小写字母,所以我想与用户沟通,而不是只是默认的消息,说格式为"无效".
所以我尝试以几种不同的方式将消息添加到lang文件中:
1)
'custom' => array(
'password.regex:' => 'Password must contain at least one number and both uppercase and lowercase letters.'
)
Run Code Online (Sandbox Code Playgroud)
2)
'custom' => array(
'password.regex' => 'Password must contain at least one number and both uppercase and lowercase letters.'
)
Run Code Online (Sandbox Code Playgroud)
3)
'custom' => array(
'password.regex:((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20})' => 'Password must contain at least one number and both uppercase and lowercase letters.'
)
Run Code Online (Sandbox Code Playgroud)
这些都没有奏效.有没有办法做到这一点?
真的很挣钱,我希望这里的人可以提供帮助.我正在写在网页API 2.一个RESTful API每当我发送给此服务的请求,响应始终都与发送Content-Type的text/plain.显然,这是不行的,我的反应必须Content-Type的application/json.我已经尝试了一些我从谷歌发现的建议,但我认为我并不了解整体情况.
为了让我的网络服务回复application/json内容,我需要做些什么特别的事吗?请注意,我希望这在整个应用程序中全局工作,所以我不是在修改给定的响应并设置其内容类型 - 我希望这是整个Web服务的默认行为:如果请求包含一个Accept为标题application/json我想我的web服务,以返回Content-Type代替text/plain.
编辑澄清:
我的响应包含一个名为"responseData"的对象,该对象应序列化为JSON并包含在正文中.我目前正在将这样的回答放在一起:
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, responseData);
return response;
Run Code Online (Sandbox Code Playgroud)
responseData是一个POCO.这个get被正确地序列化为JSON并在响应中返回 - 唯一缺少的部分是Content-Type,它被错误地设置为"text/plain".我可以在我编写的每个响应上手动更改它,但我想在全局级别配置它.
我正在尝试在Visual Studio中设置一个新项目,该项目将是使用ReactJS编写的单页应用程序的MVC 5.所以我按照ReactJS网站上的指南进行操作.
我到了你运行项目的第一部分,我得到了一个语法错误,因为JSX(浏览器似乎想把它解释为vanilla JavaScript,这非常有意义).所以我添加type="text/jsx"了脚本标签.
总的来说,我的HTML/JSX看起来像这样:
Razor视图的HTML输出
<!doctype html>
<html>
<head>
<title>Hello React</title>
</head>
<body>
<div id="content"></div>
<script src="http://fb.me/react-0.12.2.js"></script>
<script type="text/jsx" src="/Scripts/Tutorial.jsx"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Tutorial.jsx
var CommentBox = React.createClass({
render: function() {
return (
<div className="comment-box">
Hello, world! I am a CommentBox.
</div>
);
}
});
React.render(
<CommentBox />,
document.getElementById('content')
);
Run Code Online (Sandbox Code Playgroud)
我不明白 - 我做错了什么?除了添加type="text/jsx"脚本标记之外,我已经按照教程中的说法进行了操作.我假设需要包含一些东西来处理将JSX转换为vanilla JS,但是教程似乎没有提到这一点.
我在Chrome开发者工具控制台中没有收到任何错误.
我在使这个工作正常时遇到了一些麻烦.我有两节课:
public class TestClassA
{
public int? NullableIntProperty { get; set; }
}
public class TestClassB
{
public int NotNullableIntProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我设置以下映射:
cfg.CreateMap<TestClassA, TestClassB>()
.ForMember(dest => dest.NotNullableIntProperty,
opt => opt.MapFrom(src => src.NullableIntProperty));
cfg.CreateMap<TestClassA, TestClassA>()
.ForMember(dest => dest.NullableIntProperty,
opt => opt.MapFrom(src => src.NullableIntProperty));
cfg.CreateMap<TestClassB, TestClassA>()
.ForMember(dest => dest.NullableIntProperty,
opt => opt.MapFrom(src => src.NotNullableIntProperty));
cfg.CreateMap<TestClassB, TestClassB>()
.ForMember(dest => dest.NotNullableIntProperty,
opt => opt.MapFrom(src => src.NotNullableIntProperty));
Run Code Online (Sandbox Code Playgroud)
我现在已经设置了四个映射,并将测试以下场景:
int? => int
int => int?
int => int
int? => int?
Run Code Online (Sandbox Code Playgroud)
在测试类中,我然后使用这样的映射: …
我已经看过围绕这个的其他问题,我无法弄清楚如何将答案应用到我的特定情况.假设您有几个看起来像这样的模型:
public class Person
{
public int PersonId { get; set; }
}
public class Business
{
public int BusinessId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我希望能够编写几个不同的泛型方法:一个使用提供的Lambda获取模型,可能看起来像这样:
GetWhere(p => p.PersonId == 1)
Run Code Online (Sandbox Code Playgroud)
一个使用唯一键获取模型 - 为了使其灵活,我希望能够使用Lambda指定唯一键:
GetByUniqueKey(p => p.PersonId, 1)
Run Code Online (Sandbox Code Playgroud)
要么
GetByUniqueKey(b => b.BusinessId, 1)
Run Code Online (Sandbox Code Playgroud)
理想情况下GetByUniqueKey,只需要一个简化方法来构建要发送的表达式GetWhere,然后返回FirstOrDefault()结果.但这样做的逻辑完全逃避了我.我想做的事:
public IEnumerable<TModel> GetWhere(Expression<Func<TModel, bool>> whereExpression)
{
// Get from DB using expression provided
}
public TModel GetByUniqueKey<TUniqueKey>(
Expression<Func<TModel, TUniqueKey>> uniqueKeyProperty,
TUniqueKey value)
{
return GetWhere(m => uniqueKeyProperty(m) == value).FirstOrDefault();
}
Run Code Online (Sandbox Code Playgroud)
所以我想获取uniqueKeyProperty …
这看起来非常简单,我确信我在过去没有遇到任何问题,但我有一个模型,其中包含一个int数组:
查看模型
public class ExampleViewModel
{
public int ExampleProperty1 { get; set; }
public int[] ExampleProperty2 = {};
public string ExampleProperty3 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在,我将使用一些数据预加载此视图模型并显示一个表单:
控制器动作
public ViewResult Example1()
{
var viewModel = new ExampleViewModel
{
ExampleProperty1 = 888,
ExampleProperty2 = new [] { 1, 2, 3, 4 },
ExampleProperty3 = "Test string"
};
return View(viewModel);
}
Run Code Online (Sandbox Code Playgroud)
剃刀视图
@using (Html.BeginForm("example2", "class", FormMethod.Post, new {id = "ExampleForm"}))
{
@Html.HiddenForEnumerable(m => m.ExampleProperty2)
@Html.TextBoxFor(m => m.ExampleProperty1)
@Html.TextBoxFor(m => m.ExampleProperty3)
<input type="submit"/> …Run Code Online (Sandbox Code Playgroud) 我在使jQuery的Joyride插件工作时遇到了很多麻烦.我觉得它看起来很简单,但它没有发生.
我的页面上包含以下脚本:
<script src="/Scripts/jquery-1.11.0.js"></script>
<script src="/Scripts/jquery.cookie.js"></script>
<script src="/Scripts/jquery.joyride-2.1.js"></script>
<script src="/Scripts/application.js"></script>
Run Code Online (Sandbox Code Playgroud)
这三个都正确加载.在application.js我用以下方式打电话给兜风:
$(window).load(function () {
$('#tour').joyride();
});
Run Code Online (Sandbox Code Playgroud)
我的HTML看起来像这样:
<ol id="tour">
<li><p>This is the tour.</p></li>
</ol>
Run Code Online (Sandbox Code Playgroud)
出于某种原因,加载页面时绝对没有任何事情发生.我没有在控制台中出现任何错误,几乎没有任何反应.有人有这个问题吗?
我的数学似乎都是正确的,唯一不起作用的是if语句.它总是从男性价值算出数学.
javascript:
function updatespending() {
var age = parseFloat(document.forms[0].CurrentAge.value);
var value = parseFloat(document.forms[0].NetWorth.value);
if(document.forms[0].sex.value = "male") {
var yearsleft = Math.round (76 - age);
} else if(document.forms[0].sex.value = "female") {
var yearsleft = 81 - age;
}
}
Run Code Online (Sandbox Code Playgroud)
html:
<table width="433" border="1" align="center">
<tr>
<td class="liquidate">How much will you have to live off of if you liquidate all of your assets?</td>
</tr>
<tr>
<td width="381" height="68">
<table width="100%" border="0" cellpadding="4" cellspacing="4">
<tr>
<td align="right">Sex:</td>
<td>
<input type="radio" name="sex" value="male">Male<br>
<input …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的MVC5项目捆绑CSS文件,而我遇到了相对URL的问题.在你复制重复的问题按钮之前,我已经阅读了其他类似的问题,答案还没有解决我需要处理的特殊情况.
我正在处理的项目将作为常规IIS站点下的应用程序运行,这意味着它不会位于域的根目录下.当使用开箱即用的URL转换时这是一个问题,CssRewriteUrlTransform()因为它(愚蠢地,在我的拙见)中没有考虑URL中的应用程序名称,而是生成将域根作为处理的绝对路径.起始水平.假设我有以下文件夹结构:
site
'--- content
|--- css
| '--- style.css
'--- images
'--- logo.png
Run Code Online (Sandbox Code Playgroud)
如果我使用这样的相对URL:
background-image: url('../images/logo.png');
Run Code Online (Sandbox Code Playgroud)
它变成了:
background-image: url('http://domainname.com/content/images/logo.png');
Run Code Online (Sandbox Code Playgroud)
问题是,我需要它在进行转换时尊重应用程序/子目录名称.解决方案还应保留查询字符串.
background-image: url('http://domainname.com/applicationname/content/images/logo.png[?querystring]
Run Code Online (Sandbox Code Playgroud)
我发现的最接近的是AcidPAT在这个问题上提供的解决方案:MVC4 StyleBundle没有解析图像
但是这个解决方案不能为我编译,因为它似乎被response.Files视为IEnumerable,而实际上它是一个IEnumerable - 也许这从MVC4变为MVC5.
摘要
我需要一种在捆绑它们时自动将CSS中的相对路径转换为绝对路径的方法.解决方案需要了解应用程序的实际位置,因此如果应用程序安装在域的子目录下,它应该可以工作.该解决方案还需要保留相对URL中的任何查询字符串.
任何人都可以提供一些指导吗?
最接近的解决方案是这个要点:https://gist.github.com/dotnetchris/3d1e4fe9b0fa77eefc82唯一的问题是它似乎与MVC5不兼容.具体地说,从第12行开始的块开始遍历BundleFile对象列表,似乎将它们视为FileInfo对象,因此正在调用不存在的属性.
c# ×7
asp.net-mvc ×4
javascript ×3
.net ×1
.net-core ×1
ajax ×1
asp.net ×1
automapper ×1
css ×1
docker ×1
html ×1
jquery ×1
json ×1
lambda ×1
laravel ×1
php ×1
reactjs ×1
reactjs.net ×1
rest ×1
validation ×1
zurb-joyride ×1