GVKun编程网logo

asp.net-mvc – ASP.net Web API和System.Net.Http(asp.net core web api)

17

在本文中,我们将带你了解asp.net-mvc–ASP.netWebAPI和System.Net.Http在这篇文章中,我们将为您详细介绍asp.net-mvc–ASP.netWebAPI和Syste

在本文中,我们将带你了解asp.net-mvc – ASP.net Web API和System.Net.Http在这篇文章中,我们将为您详细介绍asp.net-mvc – ASP.net Web API和System.Net.Http的方方面面,并解答asp.net core web api常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的asp.net MVC 上传文件名为 System.Web.HttpPostedFileWrapper、asp.net – Googlebot导致.NET System.Web.HttpException、asp.net – System.Net.Cookie和System.Web.HttpCookie有什么区别?、asp.net-mvc – ASP.NET MVC 3:如何强制ActionLink执行HttpPost而不是HttpGet?

本文目录一览:

asp.net-mvc – ASP.net Web API和System.Net.Http(asp.net core web api)

asp.net-mvc – ASP.net Web API和System.Net.Http(asp.net core web api)

按照本文中的示例: http://blogs.msdn.com/b/yaohuang1/archive/2012/05/21/asp-net-web-api-generating-a-web-api-help-page-using-apiexplorer.aspx

我已经设置了所有内容来为我的Web API项目提供文档,但我遇到了一个问题.当我尝试使用@ api.HttpMethod时,我得到了他在文章中间描述的错误.他说你必须在web.config中手动添加对System.Net.Http,Version = 2.0.0.0程序集的引用(即使它默认位于References文件夹中),但是如果你按照他添加程序集的例子通过web.config中标记的传统方式…..你会发现它不再是4.5中的有效标记,一切都是通过AssemblyRedirects完成的.我尝试了但无济于事.任何人有这个问题或知道如何帮助改变web.config?我错过了一次会议吗?

Visual Studio 2012
MVC4 Web API项目(不是来自Nuget,VS2012附带的最终版本)

解决方法

在< system.web>下的Web.config文件中添加以下配置节点(假设您的应用程序在.NET 4.5上运行,targetFramework属性设置为4.5):
<compilation targetFramework="4.5">
  <assemblies>
    <add assembly="System.Net.Http,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" />
  </assemblies>
</compilation>

还要在< configuration>下的根级添加以下一个.节点:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" />
      <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

这应该可以解决您的问题.

asp.net MVC 上传文件名为 System.Web.HttpPostedFileWrapper

asp.net MVC 上传文件名为 System.Web.HttpPostedFileWrapper

如何解决asp.net MVC 上传文件名为 System.Web.HttpPostedFileWrapper?

AdminBlog 是我的控制器名称。 博客是我数据库中的一个表。 Foto是表格中的一列。

当我保存表单时,Foto 列值变为 System.Web.HttpPostedFileWrapper。但是其他列的值正在获得正确的值。图像也没有上传到 BlogFoto 文件

这是我的型号代码:

public partial class Blog
{
    public int BlogID { get; set; }
    public string BlogBaslik { get; set; }

    [UIHint("tinymce_full")][AllowHtml]
    public string BlogIcerik { get; set; }
    public string Foto { get; set; }
    public Nullable<int> BlogokunmaSayisi { get; set; }
    public Nullable<int> BlogokunmaSuresi { get; set; }
    public Nullable<System.DateTime> BlogTarih { get; set; }
}

并查看代码:

@using (Html.BeginForm("Create","AdminBlog",FormMethod.Post,new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()

<div>
    <h4>Yeni Blog Oluştur</h4>
    <hr />
    @Html.ValidationSummary(true,"",new { @})
    <div>
        @Html.LabelFor(model => model.BlogBaslik,htmlAttributes: new { @})
        <div>
            @Html.EditorFor(model => model.BlogBaslik,new { htmlAttributes = new { @} })
            @Html.ValidationMessageFor(model => model.BlogBaslik,new { @})
        </div>
    </div>

    <div>
        @Html.LabelFor(model => model.BlogIcerik,htmlAttributes: new { @})
        <div>
            @Html.EditorFor(model => model.BlogIcerik,new { htmlAttributes = new { @} })
            @Html.ValidationMessageFor(model => model.BlogIcerik,new { @})
        </div>
    </div>

    <div>
        @Html.LabelFor(model => model.Foto,htmlAttributes: new { @})
        <div>
            <input type="file" name="Foto"/>
            @Html.ValidationMessageFor(model => model.Foto,new { @})
        </div>
    </div>

 

    <div>
        @Html.LabelFor(model => model.BlogokunmaSuresi,htmlAttributes: new { @})
        <div>
            @Html.EditorFor(model => model.BlogokunmaSuresi,new { htmlAttributes = new { @,@placeholder = "DK cinsinden giriniz" } })
            @Html.ValidationMessageFor(model => model.BlogokunmaSuresi,new { @})
        </div>
    </div>

和控制器:

 [HttpPost]
    public ActionResult Create(Blog blog,HttpPostedFile Foto)
    {
        try
        {
            
            if (Foto != null)
            {
                
                WebImage webImage = new WebImage(Foto.InputStream);
                string newfoto = Path.GetFileNameWithoutExtension(Foto.FileName) + "-" + Guid.NewGuid() + Path.GetExtension(Foto.FileName);
               
                var filePath = "/Uploads/BlogFoto" + newfoto;

                webImage.Save(Server.MapPath(filePath));
                blog.Foto = filePath;

            }
            
           
            blog.BlogokunmaSayisi = 0;
            blog.BlogTarih = DateTime.Now;
            db.Blogs.Add(blog);
            db.SaveChanges();

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

解决方法

把博客改成这样:

public partial class Blog
{
   //...............................
   //...............................
   public HttpPostedFileBase FotoFile { get; set; }
}

此代码

<div>
  @Html.LabelFor(model => model.Foto,htmlAttributes: new { @})
  <div>
    <input type="file" name="Foto"/> 
    @Html.ValidationMessageFor(model => model.Foto,"",new { @})
  </div>
</div>

改成这样:

<div>
  @Html.LabelFor(model => model.FotoFile,htmlAttributes: new { @})
  <div>
    @Html.TextBoxFor(model => model.FotoFile,new { type = "file",@})
    @Html.ValidationMessageFor(model => model.FotoFile,null,new { @})
  </div>
</div>

并将控制器更改为:

[HttpPost]
public ActionResult Create(Blog blog)
{
      try
      {
         if (blog.FotoFile != null)
         { 
            WebImage webImage = new WebImage(blog.FotoFile.InputStream);
            string newfoto = Path.GetFileNameWithoutExtension(blog.FotoFile.FileName) + "-" + Guid.NewGuid() + Path.GetExtension(blog.FotoFile.FileName);
               
            var filePath = "/Uploads/BlogFoto" + newfoto;
            webImage.Save(Server.MapPath(filePath));
            blog.Foto = filePath;
     }
     //..................................................................
     //..................................................................
     }
     catch
     {
         return View();
     }
 }

asp.net – Googlebot导致.NET System.Web.HttpException

asp.net – Googlebot导致.NET System.Web.HttpException

我有一个ASP.NET网站混合了经典的asp(我们正在转换到.NET),我最近从.NET 1.1升级到.NET 4.0,并在IIS 7中切换到集成管道.

由于这些变化,ELMAH从传统的asp页面报告错误,实际上没有细节(和状态代码404):

System.Web.HttpException (0x80004005)
   at System.Web.CachedpathData.ValidatePath(String physicalPath)
   at System.Web.HttpApplication.PipelinestepManager.ValidateHelper(HttpContext context)

但是当我自己请求页面时,不会发生错误.所有出现在ELMAH中的错误都是由Googlebot抓取工具(用户代理字符串)引起的.

.NET如何为传统的asp页面挑选错误?这与集成管道有关吗?

任何想法,为什么错误只发生在Google抓取页面或如何获得更多的细节来找到潜在的错误?

解决方法

将其添加到您的web.config文件中:
<httpRuntime relaxedUrlToFileSystemMapping="true" />

此disables the default check确保所请求的URL符合Windows路径规则.

要重现问题,请将(URL转义的空格)添加到URL的末尾,例如http://example.org/.当搜索抓取工具遇到带有空格的错误类型的链接时,这是很常见的. < a href =“http://example.org/”>示例< / a&gt ;. HttpContext.Request.Url属性似乎修剪了尾随空间,这就是为什么像ELMAH这样的日志记录工具不会揭示实际的问题.

asp.net – System.Net.Cookie和System.Web.HttpCookie有什么区别?

asp.net – System.Net.Cookie和System.Web.HttpCookie有什么区别?

我获得了一个HTTPCookie,但需要一个Net.Cookie.它们只是你可以重铸的东西,或者我将如何去做?

解决方法

其实你有两个问题:

> System.Web.HttpCookie和System.Net.Cookie之间的区别
>如何从HTTPCookie转换为Cookie.

第1部分)
这个问题真的很有意思,我还在想为什么有两个看起来很相似的类,我最初的想法是System.Web.HttpCookie继承了System.Net.Cookie,但这不是真的直接从Object继承所以它们是不同的类,但属性匹配很多,所以这给第2部分的解决方案带来了希望.

第2部分)
我认为它可以在理论上将一个转换成另一个,因为如果你以正确的方式填充它们,它们只是对象,这里我比较了两个类时的一些分析.
单击以在新选项卡中打开以放大

更新:
System.Web用于基于服务器的应用程序,System.Net可用于基于客户端的应用程序.

一些想法:

>编写一个可以将一个对象转换为另一个对象的方法或静态类,我没有检查所有这些对象,但是名称匹配的属性,签名也匹配.
>在另一个对象中不存在的属性可以填充一些常量或您知道的值与端口号等方案匹配.

祝你好运,让我知道你是如何想出最终的解决方案,发布代码或链接.

一些链接
this post has some related code

asp.net-mvc – ASP.NET MVC 3:如何强制ActionLink执行HttpPost而不是HttpGet?

asp.net-mvc – ASP.NET MVC 3:如何强制ActionLink执行HttpPost而不是HttpGet?

是否可以强制@ Html.ActionLink()执行POST而不是GET?如果是这样,怎么样?

解决方法

ActionLink帮助器方法将呈现一个锚标记,单击它始终是一个GET请求.如果你想让它成为一个POST请求.你应该使用一点javacsript覆盖默认的behvIoUr
@ActionLink("Delete","Delete","Item",new {@id=4},new { @})

现在一些jQuery代码

<script type="text/javascript">
  $(function(){
    $("a.postLink").click(function(e){
      e.preventDefault();
      $.post($(this).attr("href"),function(data){
          // got the result in data variable. do whatever you want Now
          //may be reload the page
      });
    });    
  });    
</script>

确保您有一个HttpPost类型的Action方法来处理此请求

[HttpPost]
public ActionResult Delete(int id)
{
  // do something awesome here and return something      
}

今天的关于asp.net-mvc – ASP.net Web API和System.Net.Httpasp.net core web api的分享已经结束,谢谢您的关注,如果想了解更多关于asp.net MVC 上传文件名为 System.Web.HttpPostedFileWrapper、asp.net – Googlebot导致.NET System.Web.HttpException、asp.net – System.Net.Cookie和System.Web.HttpCookie有什么区别?、asp.net-mvc – ASP.NET MVC 3:如何强制ActionLink执行HttpPost而不是HttpGet?的相关知识,请在本站进行查询。

本文标签: