我使用ASP.NET MVC 4创建了一个博客。
问题是当我的网址不是localhost时我在帖子中看不到任何图片,因为它们的网址错误。帖子的网址可以是:
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
但是改为
url: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);
}
}
}
我必须创建一个图像hanndler ??我必须创建一个新的routeconfig ??什么样的?