我用 ASP.NET MVC 4 创建了一个博客。
问题是当我的网址不是本地主机时,我在帖子中看不到任何图片,因为它们的网址错误。帖子的网址可以是:
1.localhost/post/2015/4/name_of_post
或
2.localhost/archive/name_of_post
或
3. localhost/name_of_category/name_of_post
,
帖子里面是图片。
图片必须有这个
url:localhost/App_Files/Upload/name_of_image.jpg
但是得到了这个
网址:localhost/post/2015/4/App_Files/Upload/name_of_image.jpg
或
localhost/archive/name_of_post
或者
localhost/name_of_category/App_Files/Upload/name_of_image.jpg
.
如果您使用正确的网址,您将在浏览器中看到图片。我在创建帖子时使用 tinymce 在帖子中添加图片。图片作为文件保存在 App_Files/Upload 文件夹中。帖子在 sql 数据库中作为 html 插入一行。我使用这个 routeConfig
routes.MapRoute(
"Post",
"Archive/{year}/{month}/{title}",
new { controller = "Blog", action = "Post" }
);
uploadController 是这样的:
using HotNews.ViewModels;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace HotNews.Controllers
{
public class UploadController : Controller
{
//
// GET: /Upload/
public ActionResult Index(string id)
{
//var url = HttpRuntime.AppDomainAppVirtualPath;
var url = ConfigurationManager.AppSettings["BlogUrl"];
return View(new UploadViewModel
{
baseUrl = url,
track = id
});
}
[HttpPost]
public ActionResult UploadFile()
{
var file = Request.Files[0];
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Files/Upload"), fileName);
file.SaveAs(path);
}
return RedirectToAction("Index");
}
public ActionResult ListFiles()
{
var fileData = new List<ViewDataUploadFileResults>();
DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/App_Files/Upload"));
if (dir.Exists)
{
string[] extensions = MimeTypes.ImageMimeTypes.Keys.ToArray();
FileInfo[] files = dir.EnumerateFiles()
.Where(f => extensions.Contains(f.Extension.ToLower()))
.ToArray();
if (files.Length > 0)
{
foreach (FileInfo file in files)
{
// var baseurl = ConfigurationManager.AppSettings["BlogUrl"];
var relativePath = VirtualPathUtility.ToAbsolute("~/App_Files/Upload") + "/" + file.Name;
fileData.Add(new ViewDataUploadFileResults()
{
// url = baseurl+relativePath,
url = relativePath,
name = file.Name,
type = MimeTypes.ImageMimeTypes[file.Extension],
size = Convert.ToInt32(file.Length)
});
}
}
}
return Json(fileData, JsonRequestBehavior.AllowGet);
}
}
}
我必须创建一个图像处理程序吗?我必须创建一个新的路由配置吗?还有什么样的??