我正在尝试在asp.netcoreapp2.2 mvc this中实现project,但这给了我error。我下载了他们的代码,它符合要求。这里的Request是HttpRequestBase WebpageRenderingPage,在我的项目中似乎不可用
我尝试手动添加System.web.dll,但这不起作用。 PostDetailController.cs中使用的标头
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using MovieBlend.Models;
using MovieBlend.Services;
using PusherServer;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Authorization;
using System.Web;
Index.cshtml
@model MovieData
@{
ViewData["Title"] = Model.Title;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.16.2/axios.min.js"></script>
<script src="https://js.pusher.com/4.0/pusher.min.js"></script>
<script>
var pusher
</script>
@using Humanizer
@using StmlParsing
<style>
.user_name {
font-size: 14px;
font-weight: bold;
}
.comments-list .media {
border-bottom: 1px dotted #ccc;
}
h1 {
color: snow;
text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
}
h3 {
text-align: center;
color: snow;
text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
}
.poster_name {
text-align: left;
color: darkred;
text-decoration: underline
}
section {
width: 100%;
float: left;
}
.banner-section {
background-image: url("https://static.pexels.com/photos/373912/pexels-photo-373912.jpeg");
background-size: cover;
height: 380px;
left: 0;
position: absolute;
top: inherit;
background-position: 0;
}
.post-title-block {
padding: 100px 0;
}
.post-title-block h1 {
color: #fff;
font-size: 85px;
font-weight: bold;
text-transform: capitalize;
}
.post-title-block li {
font-size: 20px;
color: #fff;
}
.image-block {
float: left;
width: 100%;
margin-bottom: 10px;
}
.footer-link {
float: left;
width: 100%;
background: #222222;
text-align: center;
padding: 30px;
}
.footer-link a {
color: #A9FD00;
font-size: 18px;
text-transform: uppercase;
}
.div-post {
margin-top: 45px;
}
</style>
<div id="app">
<section class="banner-section"></section>
<section class="post-content-section">
<div class="container">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 post-title-block">
<h1 class="text-center">@Model.Title</h1>
<h3> @Model.Release | @Model.Language | @Model.Genre</h3>
</div>
<div class="col-lg-9 col-md-9 col-sm-12 div-post">
<p>
<h6 class="poster_name">Posted By @Model.User_name</h6>
<h7 class="poster_name">Posted @Model.Postedate.Humanize()</h7>
<br />@Html.Raw(@StmlParser.Parse(Model.Description))
</p>
</div>
</div>
</div>
</section>
<div class="container">
<br />
<p>You Comment</p>
<form onsubmit="return false;">
<input type="text" id="Name" placeholder="Your Name" v-model="comment.Name" class="form-control" />
<textarea rows="3" placeholder="Your Comment" id="Body" v-model="comment.Body" class="form-control"></textarea>
<button class="btn btn-success" v-on:click="submit_comment()">save</button>
</form>
</div>
<div class="container">
<div class="row">
<div class="page-header">
<h3><small class="pull-right">{{comments.length}}</small>Comments</h3>
</div>
<div class="comments-list">
<div class="media-body">
<h4 class="media-heading user_name">{{com.name}}</h4>
{{com.body}}
</div>
</div>
</div>
</div>
</div>
<script>
var pusher = new Pusher('7ba0ddfcfbfe565ee8ee', {
cluster: 'ap2'
});
var my_channel = pusher.subscribe('asp_channel');
var app = new Vue({
el: '#app',
data: {
comments: [],
comment: {
Name: '',
Body: '',
BlogPostID: @Model.Id
}
},
created: function() {
this.get_comments();
this.listen();
},
methods: {
get_comments: function() {
axios.get('@Url.Action("Comments", "PostDetail", new { id = @Model.Id }, protocol: Request.Url.Scheme)')
.then((response) => {
this.comments = response.data;
});
},
listen: function() {
my_channel.bind("asp_event", (data) => {
if (data.BlogPostID == this.comment.BlogPostID) {
this.comments.push(data);
}
})
},
submit_comment: function() {
axios.post('@Url.Action("AddComment", "PostDetail", new {}, protocol: Request.Url.Scheme)', this.comment)
.then((response) => {
this.comment.Name = '';
this.comment.Body = '';
alert("Comment Submitted");
});
}
}
});
</script>
在这行中给我错误
axios.post('@Url.Action("AddComment", "PostDetail", new {}, protocol: Request.Url.Scheme)', this.comment)
和
axios.get('@Url.Action("Comments", "PostDetail", new { id = @Model.Id }, protocol: Request.Url.Scheme)')
陈述请求不可用
csporj文件
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<UserSecretsId>aspnet-MovieBlend-2E001261-E25D-4A77-9170-29F16D765657</UserSecretsId>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CoreCompat.System.Drawing" Version="1.0.0-beta006" />
<PackageReference Include="Humanizer" Version="2.6.2" />
<PackageReference Include="Microsoft.AspNet.Mvc" Version="5.2.7" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="PusherServer" Version="4.1.0" />
<PackageReference Include="StmlParser" Version="1.0.2" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Web">
<HintPath>E:\Downloads\Compressed\System.Web\System.Web.dll</HintPath>
</Reference>
<Reference Include="System.Web.Abstractions">
<HintPath>E:\Downloads\Compressed\System.Web.Abstractions\System.Web.Abstractions.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
如何解决这个问题?
答案 0 :(得分:1)
您需要在页面顶部添加@page
指令,以指示它是razor page。页面顶部应如下所示:
@page
@model MovieData
@{
ViewData["Title"] = Model.Title;
}
然后,您应该可以访问Request属性。我做了类似的事情(但只是调用Request.Scheme):
var _url = '@Url.Action("UploadFile", "Data", new { projectId = Model.ID }, Request.Scheme)';