【问题标题】:Using MVC Controller to replace .ashx使用 MVC 控制器替换 .ashx
【发布时间】:2012-05-11 06:12:38
【问题描述】:

我需要一个控制器来处理文件上传。是否可以让处理程序直接将文本打印到页面而不是 return view(); ?

public ActionResult Upload(HttpContext context)
        {
            HttpPostedFile file = context.Request.Files["fileData"];

            Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey;
            string userID = userGuid.ToString();

            string targetLocation = "D:\\inetpub\\wwwroot\\RTDOTNETMEMBER\\audio\\songs\\mp3\\" + userID + "\\" + file.FileName;

            file.SaveAs(targetLocation);

            Response.Write("Testing");
}

【问题讨论】:

  • 请多解释一下,你可以用return Content("bla")返回文本,但它可能不是你要找的东西。
  • 谢谢,这确实是我想要的。我对此有点陌生。

标签: c# asp.net asp.net-mvc asp.net-mvc-3


【解决方案1】:

也许使用 ContentResult 就可以了:

return new ContentResult() { 
    Content = "Testing", 
    ContentEncoding = System.Text.Encoding.UTF32, 
    ContentType = "text/plain" 
};

【讨论】:

  • Gorans 上面的评论将是一个更标准的语法引用 this.Content 直接在控制器上返回 ContentResult
【解决方案2】:

只需将您的操作方法的返回类型更改为字符串并返回一个字符串。它看起来像这样:

    public string ReturnString()
    {
        return "Just a string";
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-09
    • 2016-01-25
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多