2014-04-16 14 views
5

Tôi muốn làm nổi bật sự khác biệt giữa hai tệp trong asp.net. Sau khi tìm kiếm trên web, tôi đã chọn Diffplex APi. Tôi là một người khởi xướng. Tôi cần một số hướng dẫn về cách thực hiện nó? Tôi đã thêm các thư viện tham khảo và đó là tất cả những gì tôi có thể hiểu được. Không có tài liệu nào về Api. Tôi chưa từng sử dụng Apis trước đây.Bắt đầu với Diffplex

Trả lời

6

Đây là một ví dụ đơn giản từ "tài liệu" (tức là mã nguồn) sẽ giúp bạn bắt đầu.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using DiffPlex; 
using DiffPlex.DiffBuilder; 
using DiffPlex.DiffBuilder.Model; 
using System.Text; 


namespace DiffPlexTest.Controllers 
{ 
    public class HomeController : Controller 
    { 
     public ActionResult Index() 
     { 
      StringBuilder sb = new StringBuilder(); 

      string oldText = @"We the people 
       of the united states of america 
       establish justice 
       ensure domestic tranquility 
       provide for the common defence 
       secure the blessing of liberty 
       to ourselves and our posterity"; 
      string newText = @"We the peaple 
       in order to form a more perfect union 
       establish justice 
       ensure domestic tranquility 
       promote the general welfare and 
       secure the blessing of liberty 
       to ourselves and our posterity 
       do ordain and establish this constitution 
       for the United States of America"; 

      var d = new Differ(); 
      var builder = new InlineDiffBuilder(d); 
      var result = builder.BuildDiffModel(oldText, newText); 

      foreach (var line in result.Lines) 
      { 
       if (line.Type == ChangeType.Inserted) 
       { 
        sb.Append("+ "); 
       } 
       else if (line.Type == ChangeType.Deleted) 
       { 
        sb.Append("- "); 
       } 
       else if (line.Type == ChangeType.Modified) 
       { 
        sb.Append("* "); 
       } 
       else if (line.Type == ChangeType.Imaginary) 
       { 
        sb.Append("? "); 
       } 
       else if (line.Type == ChangeType.Unchanged) 
       { 
        sb.Append(" "); 
       } 

       sb.Append(line.Text + "<br/>"); 
      } 

      ViewData["old"] = oldText; 
      ViewData["new"] = newText; 
      ViewData["result"] = sb.ToString(); 

      return View(); 
     } 
    } 
} 
+0

Cảm ơn, chính xác những gì tôi đang tìm kiếm – JanR

+2

vĩ đại - tôi đã xây dựng một công cụ diff trên đầu trang của DiffPlex rằng hoạt động khá tốt vì vậy tôi nghĩ rằng bạn sẽ gặp may mắn với nó. –

+0

Diffplex có tùy chọn hợp nhất gốc không? – JanR