1

我正在关注这篇文章以建立简单的网站。我有一个带有对象的数据库,每个对象代表 dist 上的文件和几个属性(大小、名称等),我有几个问题:

  1. 在运行我的应用程序后添加本文中的控制器(分钟 1:45)后,它不是导航到我的主页而是导航到此索引页面,并且只有在按下主页按钮后我才能看到我的主页 - 如何修复它?

  2. 在此示例中,每个人都有几个操作:删除、更新、详细信息...我只想选择Download将此文件下载到我的系统中(所有文件位置都在具有访问权限的网络文件夹中)-我该怎么做?

索引.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 &raquo;</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 &raquo;</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 &raquo;</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 &raquo;</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 &raquo;</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 &raquo;</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 &raquo;</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);
    }
}

全球.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);
    }
}
4

1 回答 1

0

当您在新控制器中编写代码并使用它来显示站点的默认主页时,您需要使用此更改文件中global.asaxRouteConfig文件中的路由(无论您在哪里注册路由),

    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);
}
于 2013-11-08T11:07:55.507 回答