我正在关注this文章,以便构建简单的网站。 我有一个带有对象的数据库,每个对象代表dist上的文件和几个属性(大小,名称等......),我有几个问题:
在运行我的应用程序后添加像本文(最小1:45)中的控制器后,它不是导航到我的主页,而是导航到这个索引页面,只有在按下主页按钮后我才能看到我的主页 - 如何解决它?
在这个例子中,每个人都有几个动作:删除,更新,细节......
我只想让选项Download
将此文件下载到我的系统中(所有文件位置都在网络文件夹中,可以访问) - 我该怎么做?
Index.schtml:
@{
ViewBag.Title = "Home Page";
}
<div class="hero-unit">
<h1>Automation Captures <image>
<img src="~/img/wireshark_logo.png" /></image></h1>
<p class="lead">Automation captures made by running robors.</p>
<p><a href="http://asp.net" class="btn btn-primary btn-large">Learn more »</a></p>
</div>
<div class="row">
<div class="span4">
<h2>button1</h2>
<p>ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that
enables a clean separation of concerns and gives you full control over markup
for enjoyable, agile development.</p>
<p><a href="/WebMail " class="btn btn-primary btn-large">Click here »</a></p>
</div>
<div class="span4">
<h2>button2</h2>
<p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p>
<p><a href="http://asp.net" class="btn btn-primary btn-large">Click here »</a></p>
</div>
<div class="span4">
<h2>button3</h2>
<p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p>
<p><a href="http://asp.net" class="btn btn-primary btn-large">Click here »</a></p>
</div>
</div>
<div class="row">
<div class="span4">
<h2>button4</h2>
<p>
ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that
enables a clean separation of concerns and gives you full control over markup
for enjoyable, agile development.
</p>
<p><a href="http://asp.net" class="btn btn-primary btn-large">Click here »</a></p>
</div>
<div class="span4">
<h2>button5</h2>
<p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p>
<p><a href="http://asp.net" class="btn btn-primary btn-large">Click here »</a></p>
</div>
<div class="span4">
<h2>button6</h2>
<p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p>
<p><a href="http://asp.net" class="btn btn-primary btn-large">Click here »</a></p>
</div>
</div>
家庭控制器
public class NewController : Controller
{
private MyObjectDBContext db = new MyObjectDBContext();
// GET: /WebMail/
public ActionResult Index()
{
return View(db.MyObjects.ToList());
}
// GET: /WebMail/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
MyObject MyObject = db.MyObjects.Find(id);
if (MyObject == null)
{
return HttpNotFound();
}
return View(MyObject);
}
// GET: /WebMail/Create
public ActionResult Create()
{
return View();
}
我的新控制器
public class NewController : Controller
{
private MyObjectDBContext db = new MyObjectDBContext();
// GET: /MyObject/
public ActionResult Index()
{
return View(db.MyObjects.ToList());
}
// GET: /MyObject/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
MyObject MyObject = db.MyObjects.Find(id);
if (MyObject == null)
{
return HttpNotFound();
}
return View(MyObject);
}
// GET: /MyObject/Create
public ActionResult Create()
{
return View();
}
// POST: /MyObject/Create
// To protect from over posting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
//
// Example: public ActionResult Update([Bind(Include="ExampleProperty1,ExampleProperty2")] Model model)
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(MyObject MyObject)
{
if (ModelState.IsValid)
{
db.MyObjects.Add(MyObject);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(MyObject);
}
// GET: /MyObject/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
MyObject MyObject = db.MyObjects.Find(id);
if (MyObject == null)
{
return HttpNotFound();
}
return View(MyObject);
}
// POST: /MyObject/Edit/5
// To protect from over posting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
//
// Example: public ActionResult Update([Bind(Include="ExampleProperty1,ExampleProperty2")] Model model)
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(MyObject MyObject)
{
if (ModelState.IsValid)
{
db.Entry(MyObject).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(MyObject);
}
// GET: /MyObject/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
MyObject MyObject = db.MyObjects.Find(id);
if (MyObject == null)
{
return HttpNotFound();
}
return View(MyObject);
}
// POST: /MyObject/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
MyObject MyObject = db.MyObjects.Find(id);
db.MyObjects.Remove(MyObject);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
Global.ASPX
public class MvcApplication : System.Web.HttpApplication
{
private CaptureDBContext db = new CaptureDBContext();
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
答案 0 :(得分:0)
由于您已在新控制器中编写代码并使用它来显示网站的默认主页,因此您需要在global.asax
或RouteConfig
文件中更改路径(无论您注册路径是什么) )有了这个,
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "New", action = "Index",
id = UrlParameter.Optional }
);
要下载文件,您需要控制器操作才能返回FileResult
public FileResult Download()
{
byte[] fileBytes =/*fetch your file here and read it*/;
string fileName = "example";
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet,
fileName);
}