اشتراک‌ها
اشتباهات متداول با ASP.NET MVC

I've always said that when a new version of something comes out, you should always "let the scouts take the arrows." Basically, let the testers see if everything is ok before giving the go-ahead to start using it. 

اشتباهات متداول با ASP.NET MVC
نظرات مطالب
شروع به کار با AngularJS 2.0 و TypeScript - قسمت نهم - مسیریابی
نکات ارتقاء به نگارش RTM
همان «نکات ارتقاء به نگارش RC5 » در اینجا هم برقرار هستند. فقط نام فایل app.routes.ts به app.routing.ts تغییر یافته‌است.
تغییرات پروژه را در اینجا می‌توانید دنبال کنید.

چند مطلب تکمیلی
Using the New Release of Angular 2’s Router 3.0.0 
Angular 2 Routing With Modules  
نظرات مطالب
تبدیل PDF به تصویر با استفاده از API توکار Window 8.1 در برنامه‌های غیر مترو دات نت
یک نکته‌ی تکمیلی
اگر با استفاده از مطالب فوق قصد داشته باشید در WPF یک PDF Viewer درست کنید، می‌توان از متد ذیل استفاده کرد:
        private async Task<List<System.Windows.Media.Imaging.BitmapImage>> getPdfPageImages()
        {
            var results = new List<System.Windows.Media.Imaging.BitmapImage>();
            using (var randomAccessStream = File.Open("PieChartPdfReport.pdf", FileMode.Open).AsRandomAccessStream())
            {
                var pdfDocument = await PdfDocument.LoadFromStreamAsync(randomAccessStream);
                for (uint i = 0; i < pdfDocument.PageCount; i++)
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        using (var stream = memoryStream.AsRandomAccessStream())
                        {
                            using (var page = pdfDocument.GetPage(i))
                            {
                                // Set render options
                                var renderOptions = new PdfPageRenderOptions
                                {
                                    BackgroundColor = Colors.LightGray,
                                    DestinationHeight = (uint)(page.Size.Height * 10)
                                };

                                await page.RenderToStreamAsync(stream); //, renderOptions);
                                await stream.FlushAsync();

                                var bitmapImage = new System.Windows.Media.Imaging.BitmapImage();
                                bitmapImage.BeginInit();
                                //Without this, BitmapImage uses lazy initialization by default and the stream will be closed by then.
                                bitmapImage.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
                                bitmapImage.StreamSource = memoryStream;
                                bitmapImage.EndInit();

                                results.Add(bitmapImage);
                            }
                        }
                    }
                }
            }
            return results;
        }
بعد برای استفاده از BitmapImageهای حاصل از  آن، برای مثال نمایش اولین صفحه در یک کنترل Image استاندارد، می‌توان نوشت:
        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            var images = await this.getPdfPageImages();
            ImagePdf.Source = images.First();
        }
نظرات مطالب
Globalization در ASP.NET MVC - قسمت هفتم
با تشکر از زحمات شما
اینجا بیان شده زمانیکه از اسمبلی دیگری برای resource‌ها استفاده میکنید فقط میتوان global resources  را پوشش داد.
بنابراین برای استفاده از کلاس LocalDbResourceProvider  بایستی تغییراتی صورت بگیره.
چونکه همیشه این متد
using System.Web.Compilation;

namespace DbResourceProvider
{
  internal class DbResourceProviderFactory : ResourceProviderFactory
  {
    #region Overrides of ResourceProviderFactory

    public override IResourceProvider CreateGlobalResourceProvider(string classKey)
    {
      return new GlobalDbResourceProvider(classKey);
    }

    ...
  }
}

اجرا میشود.
نظرات مطالب
مقدمه ای بر AutoMapper
با سلام میخواستم ببینم آیا امکان استفاده از automapper بدین صورت هم وجود داره؟
 public void Edit_news_ajax(News news)
        {
            using (var Context = new ProCamContext())
            {
                var q=Context.News.Find(news.id);
                AutoMapper.Mapper.Map(news,q);
               //NewsRepository.EditNews(news.id, q);
                Context.SaveChanges();
            }//end news
        }
با تشکر
نظرات مطالب
الگوی PRG در ASP.NET MVC
البته روش توضیح داده شده همان روش متداول PRG است. اگر حتما نیاز به 303 دارید به روش زیر باید عمل کرد:
using System.Web.Mvc;

namespace TestMvcPRG.Helper
{
    public class Redirect303 : ActionResult
    {
        private string _url;

        public Redirect303(string url)
        {
            _url = url;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.StatusCode = 303; // redirect using GET
            context.HttpContext.Response.RedirectLocation = _url;
        }
    }

    public abstract class BaseController : Controller
    {
        public Redirect303 Redirect303(string actionName)
        {
            return new Redirect303(Url.Action(actionName));
        }

        public Redirect303 Redirect303(string actionName, object routeValues)
        {
            return new Redirect303(Url.Action(actionName, routeValues));
        }

        public Redirect303 Redirect303(string actionName, string controllerName)
        {
            return new Redirect303(Url.Action(actionName, controllerName));
        }

        public Redirect303 Redirect303(string actionName, string controllerName, object routeValues)
        {
            return new Redirect303(Url.Action(actionName, controllerName, routeValues));
        }
    }
}
و بعد برای استفاده:
using System.Web.Mvc;
using TestMvcRPG.Helper;

namespace TestMvcPRG.Controllers
{
    public class HomeController : BaseController
    {
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(string data)
        {
            if (ModelState.IsValid)
            {
                return Redirect303("Index"); // post-redirect-get
            }
            return View();
        }
    }
}
نظرات مطالب
آشنایی با NHibernate - قسمت هشتم
سلام.
فرض کنید همچین کدی نوشته ایم:
using (var repository = new Repository())
{
try
{
// Some Code ...
repository.Update(myClass);
// Or
repository.Delete(myClass);
return true;

}
catch (Exception)
{
MessageBox.Show("خطا در ویرایش یا حذف");
}
}
در صورت بروز خطا هیچ وقت بلاک catch اجرا نمی شود.
البته هنگام return true متد Dispose کلاس Repository اجرا می شود.(نوش دارو پس از مرگ سهراب...)
نظرات مطالب
الگویی برای مدیریت دسترسی همزمان به ConcurrentDictionary
Lazy را به قسمتی از درون Func اعمال کردید. کل Func باید Lazy شود تا درست کار کند.
using System;
using System.Collections.Concurrent;
using System.Threading;

namespace LazyDic
{
    public class LazyConcurrentDictionary<TKey, TValue>
    {
        private readonly ConcurrentDictionary<TKey, Lazy<TValue>> _concurrentDictionary;
        public LazyConcurrentDictionary()
        {
            _concurrentDictionary = new ConcurrentDictionary<TKey, Lazy<TValue>>();
        }

        public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
        {
            var lazyResult = _concurrentDictionary.GetOrAdd(key,
             k => new Lazy<TValue>(() => valueFactory(k), LazyThreadSafetyMode.ExecutionAndPublication));
            return lazyResult.Value;
        }

        public TValue AddOrUpdate(TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
        {
            var lazyResult = _concurrentDictionary.AddOrUpdate(
                key,
                new Lazy<TValue>(() => addValue),
                (k, currentValue) => new Lazy<TValue>(() => updateValueFactory(k, currentValue.Value),
                                                      LazyThreadSafetyMode.ExecutionAndPublication));
            return lazyResult.Value;
        }

        public TValue AddOrUpdate(TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
        {
            var lazyResult = _concurrentDictionary.AddOrUpdate(
                key,
                k => new Lazy<TValue>(() => addValueFactory(k)),
                (k, currentValue) => new Lazy<TValue>(() => updateValueFactory(k, currentValue.Value),
                                                      LazyThreadSafetyMode.ExecutionAndPublication));
            return lazyResult.Value;
        }

        public int Count => _concurrentDictionary.Count;
    }
}
پاسخ به بازخورد‌های پروژه‌ها
تغییر نام یک فایل
با سلام مجدد
کسی جواب نداد خودم دست به کار شدم.

در مورد مشکل بالا در واقع یک Partial View آماده کردند به نام _breadCrumb.cshtmlکه کدش در پایین هست.
 

@using System.IO;
@{
    var pathsplited = new string[] { };
    if (ViewBag.CurrentPath != null)
    {
        pathsplited = ViewBag.CurrentPath.Split(Path.DirectorySeparatorChar);
    }
    string Host = Request.Url.Authority + "/" + MvcFileManager.Services.Helper.PathBrowseinWebConfig;
    string pathcombine = "";
}
@Html.ActionLink(Host , "Browse", new { path = pathcombine })  /

@if (pathsplited != null)
{
    foreach (var s in pathsplited)
    {
        if (s != "")
        {
            pathcombine += s + @"\";
            @Html.ActionLink(s, "Browse", new { path = pathcombine })
            @Html.Raw(" / ")   ;
        }

    }
}

و کد بالا را باز نویسی کردم به کد پایین
@using System.Diagnostics
@using MvcFileManager.Services
@{
    var pathsplited = new string[] { };
    if (ViewBag.CurrentPath != null)
    {
        pathsplited = ViewBag.CurrentPath.Split(Path.DirectorySeparatorChar);
    }

    Debug.Assert(Request.Url != null, "Request.Url != null");
    string host = Request.Url.Authority + "/" + Helper.PathBrowseinWebConfig;
    string pathcombine = "";
}
@Html.ActionLink(host , "Browse", new { path = pathcombine })  

@if (pathsplited != null)
{
    foreach (var s in pathsplited)
    {
        if (s.Trim() != "")
        {
            @Html.Raw(" / ")
            pathcombine += s;
            pathcombine += Helper.FileFolderCheck(pathcombine) == FileFolderStatus.File ? "" : @"\";
            @Html.ActionLink(s, "Browse", new { path = pathcombine })
        }
    }
    @Html.Raw(Helper.FileFolderCheck(ViewBag.CurrentPath)==FileFolderStatus.File ? "" : (" / "))
}

که مشکل برطرف شد.
و یه مورد هایی در استفاده از / برای جدا کردن فایل‌ها و پوشه‌ها بود که اصلاح شد.
با تشکر