針對查詢「mvc」依關聯性排序顯示文章。依日期排序 顯示所有文章
針對查詢「mvc」依關聯性排序顯示文章。依日期排序 顯示所有文章

2014年11月4日 星期二

asp.net MVC 多國語系設定



參考文件

ASP.NET MVC 讓 jQuery Validation Plugin 動態切換顯示訊息語系

ASP.NET MVC And Localization

ASP.NET Boilerplate Localization

ASP.NET MVC 你一定要知道怎麼創立HTML Helper

Attributes Tutorial

Localization in ASP.NET MVC 4 – Upgraded

asp.net mvc multilanguage urls/routing  =>要客製一個 controller而且所有新建的controller都要繼承它

ASP.NET MVC Localization - Routing  => controller不用繼承客制的 controller (目前我使用的)

目前我參考的這一個算是蠻好設定的而且有範例碼在 GitHub,自己也有學到新東西

比如 RouteCollectionExtensions,建立一個靜態的類別,在 RouteConfig就可以使用它的方法

public static class RouteCollectionExtensions,而在作語系切換時只要將 cookie locale的值設定為指定語系即可

ASP.NET MVC + ANGULARJS SPA + LOCALIZATION


ASP.NET MVC With AngularJS

如果有使用 angularjs 並且將網頁設定成 SPA方式的話,我使用的方式還要再作修改,要不然angularjs route碰到下列情況就會有問題

 .when('/routeTwo/:donuts', {
            templateUrl: function(params) { return '/routesDemo/two?donuts=' + params.donuts; }
        })


因為在原作者再作 redirect new url時並沒有把 request params (querystring)的參數帶入,所以要在 RedirectHandler的 ProcessRequest內再把參數帶入

 public void ProcessRequest(HttpContext context)
 {
            NameValueCollection queryParameters = context.Request.QueryString;
            string qTring = string.Empty;

            if (queryParameters.Count > 0)
            {
                qTring = "?" + queryParameters.ToString();
           
            }
 
            context.Response.Redirect(this._newUrl+ qTring);
   }


2013年4月18日 星期四

MongoDB 和 Asp.Net MVC

A MONGODB TUTORIAL USING C# AND ASP.NET MVC 使用 ASP.NET MVC 3和 MONGODB實作了一個 BLOG網站,大家可以試試看,我自己還在消化,有幾點可以和大家分享

我使用是 APS.NET MVC 4 和 MONGODB C# DRIVER 1.8.1.20和一個 MONGODB 是放在 SERVER上,所以使用宓碼連結

連結 MONGO的寫法要改成如下

var client = new MongoClient("mongodb://userName:passWord@ipaddress/dbName");
var server = client.GetServer();
var db = server.GetDatabase("dbName");

可以成功執行後,我想要讓 CKEDITOR 可以上傳圖片,就 ASP.NET MVC來說,還是要使用 CK FINDER FOR ASP.NET的版本

可以在 CKEDITOR 的 config.js 內加上
config.filebrowserImageBrowseUrl = '/Scripts/ckfinder/ckfinder.html?type=Images';
config.filebrowserUploadUrl= '/Scripts/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Files';
config.filebrowserImageUploadUrl= '/UploadImage/Upload';  //controller/actionName

在 CKFINDER的 config.ascx

public override bool CheckAuthentication() 依權限 return true;

public override void SetConfig()
我的 CKFINDER是放在 Scripts的目錄,所以 BaseUrl = "~/Scripts/ckfinder/userfiles/";


客制的 UPLOAD CONTROLLER
public ActionResult Upload(HttpPostedFileBase upload, string CKEditorFuncNum, string CKEditor, string langCode)
    {
        string url; // url to return
        string message; // message to display (optional)
        //CODE FOR SAVE IMAGE HERE
        url =  "/" + path + "/" + filename;
      string output = @"";

            return Content(output);
        }
參數有三個CKEditor=editor1&CKEditorFuncNum=1&langCode=en

CK Finder for asp.net V2.3.1 download


參考網址
A MONGODB TUTORIAL USING C# AND ASP.NET MVC

CSharp Driver Tutorial

Authenticate to MongoDB with the C# Driver

Integration of CkFinder with Ckeditor failed in my Asp.net mvc3.How can i successfully integrate it?

Integrating CKEditor with a Custom File Browser

Splitting DateTime - Unit Testing ASP.NET MVC Custom Model Binders

ASP.NET MVC Model Binding and Data Annotation

MongoDB实战开发

PHP實作 Ckeditor+Ckfinder檔案上傳 動態指定儲存位置


其它..

在 Global.asax.cs 的 protected void Application_Start() 加上

 ModelBinders.Binders.Add(typeof(ObjectId), new ObjectIdBinder());  //自動產生 mongoID

monogo基本指令

show dbs
show collections
use --dbName  

db.--collectionsName.find()

mogodb C# driver 請直接用 negut就可以找的到



2012年9月10日 星期一

ASP.NET MVC 3 APP_Code 已不使用

目前 MVC 專案不需要這個目錄,如果你手動建立這個目錄並產生一個類別後作 compile  ,好像不會有任何作用,一般的作法好像要建立 Class Library,然後再 MVC Project 作  reference的動作。 而 APP_Code好像也可以拿來放 Helper的程式,不過我是新手還沒用到。


參考
http://stackoverflow.com/questions/10620947/asp-net-mvc-3-app-code-folder
http://stackoverflow.com/questions/4866462/having-razor-helper-in-app-code-folder-using-asp-net-mvc-3
http://blog.miniasp.com/post/2010/02/22/ASPNET-MVC-Developer-Note-Part-15-Running-under-WebSite-Project.aspx
http://blog.sanc.idv.tw/2011/08/aspnet-mvc-razorhelper.html

2014年11月4日 星期二

ASP.NET MVC With AngularJS

 @Scripts.Render("~/bundles/angularjs")   要放在 angular.module使用之前

使用 controller的寫法

1.2
 function HelloController($scope) {
     $scope.greeting = { text: 'Hello' };
 }

1.3
var myApp = angular.module('myApp', []);

     myApp.controller('HelloController', ['$scope', function ($scope) {
         $scope.greeting = { text: 'Hello' };
     } ]);


or 

 function HelloController($scope) {
         $scope.greeting = { text: 'Hello' };
  }

// The $inject property of every controller (and pretty much every other type of object in Angular) needs to be a string array equal to the controllers arguments, only as strings
HelloController.$inject = ['$scope'];

 var myApp = angular.module('myApp', []);
 myApp.controller('HelloController', HelloController);


SPA 注意事項
1.要設定 SPA要先加入 angular-route.min.js

 //AngularJS
 bundles.Add(new ScriptBundle("~/bundles/angularjs").Include(
                        "~/Scripts/angular.min.js"
                       , "~/Scripts/angular-route.min.js"
                     
                        ));

2.設定連結
   a href="/#/routeOne"

   如果 default網頁是 Index.cshtml,可以寫成如上的連結,但如果不是,比如有一個網頁叫 SPATest.cshtml放在 Home目錄底下的話,連結修改如下:
 
   a href="/Home/SPATest/#/routeOne"

3.加上一個   <div ng-view></div>在你的 Index.cshtml (SPA的首頁)上

4.設定 angulasjs route,可儲存在 javascript檔
 
     //routing for angularjs
     var configFunction = function ($routeProvider) {
         $routeProvider.
        when('/routeOne', {
            templateUrl: '/RoutesDemo/one'
        })
        .when('/routeTwo', {
            templateUrl: '/RoutesDemo/two'
        })
       .when('/routeThree', {
            templateUrl: '/RoutesDemo/three'
        });

     }
     configFunction.$inject = ['$routeProvider'];

     myApp.config(configFunction);

5. 建立 contorller
     public class RoutesDemoController : Controller
    {
        //
        // GET: /RouttesDemo/

        public ActionResult One()
        {
            ViewBag.test = 1;
            return PartialView("SPAPartialTest");
        }

        public ActionResult Two(int donuts = 1)
        {
            ViewBag.Donuts = donuts;
            ViewBag.test = 1;
            return PartialView("Two");
        }
        public ActionResult Three()
        {
            ViewBag.test = 3;
            return PartialView("SPAPartialTest");
        }

    }

6.建立 partialview


Hands On Lab: Build a Single Page Application (SPA) with ASP.NET Web API and Angular.js

Using AngularJs, ASP.NET MVC, Web API and EntityFramework to build NLayered Single Page Web Applications

【AngularJs + ASP.NET MVC】使用AntularJs快速建立ASP.NET MVC SPA網站

Getting started with AngularJS and ASP.NET MVC - Part One
Getting started with AngularJS and ASP.NET MVC - Part Two

AngularJS 開發 ASP.NET MVC

What's the difference between “return View()” and “return PartialView()”

2017年8月17日 星期四

Asp.Net Core 2.0 Razor Pages - MVC外的新選擇

如果你在學 Asp.Net MVC時對其目錄結構,不是那麼容易了解的話,現在在 Core 2.0提供一個 Razor Pages讓以前寫 Asp.Net Web Form的使用者能夠更容易上手

相關文章參考:
ASP.NET Razor Pages vs MVC – How do Razor Pages fit in your toolbox?
Introduction to Razor Pages in ASP.NET 

為什麼比較容易懂 ?

  1. 用 code behind的方式,在 Pages目錄下,一個 .chhtml 對應到一個 .cs檔案
  2. 以前在 Controller目錄下,一個 Controller Class內部不止針對網頁回傳而且也會有ajax回傳資料的方法,但在 Razor Page的目錄下就只會針對其 csthml實作 OnGet, OnPost方法,比較單純
  3. 使用 annotation [BindingProperty]作 databinding, cs file本身就是 model file

    reference from ASP.NET Razor Pages vs MVC – How do Razor Pages fit in your toolbox?

2016年11月22日 星期二

Asp.Net Core MVC Paging使用了 cloudscribe.Web.Pagination

之前開發 MVC 5專案參考了 ASP.NET MVC 資料分頁 MVCPaging 2.0 應用分享文章,使用了 MVCPaging開發分頁需要的程式,但到 Core之後 MVCPaing官網建議要使用   cloudscribe.Web.Pagination library。

使用 Nuget安裝後要先設定
Startup.cs 要加上 services.AddCloudscribePagination();
_ViewImports.cshtml 要加上@addTagHelper "*, cloudscribe.Web.Pagination"

程式是最大的改變應該是在 View的部份,原本在顯示 page info的部份改寫如下:

<cs-pager asp-action="Index" asp-controller="Paging" 
     cs-pagenumber-param="page" 
     cs-paging-pagenumber="@Model.PageNumber" 
     cs-paging-pagesize="@Model.PageSize" 
     cs-paging-totalitems="@Model.TotalItemCount">
</cs-pager>

測試後的結果應該是沒問題,後續還是要測試每個 tag attribute




2012年9月6日 星期四

IIS6 設定 for MVC3

今天寫了第一隻 MVC3 的程式,用 VS2010開發的,還是屬於不熟的狀態

不過想放至測試機時出現


Directory Listing Denied


This Virtual Directory does not allow contents to be listed.

找了文件
萬用字元加上
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll
並把 確認該檔是否存在 選項取消即可



















參考文件
http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx
http://www.asp.net/mvc/mvc3
http://blog.masonchu.com/2012/03/iis-6-aspnet-mvc-directory-listing.html



但是asp.net MVC3 installer裝在第二台時出現了
SetupResources.dll missing from 1028 directory 的問題,還在決解中.................
找到解決方式了,因為 server裝了 patch之後沒重開,後來重新開机後就可以安裝了

2014年10月29日 星期三

如何在 ASP.NET MVC 4 使用 CDN上的 java script 和 css

一般在 app_start的 BundleClass 會如下的程式碼,以讀取自己下載下來的 java script & css

 bundles.Add(new StyleBundle("~/Content/sweetalsertforbs_css").Include(
                       "~/Content/sweet-alert.css"
                       ));

 bundles.Add(new ScriptBundle("~/bundles/sweetalsertforbs_js").Include(
                       "~/Scripts/sweet-alert.js"));

如果 ASP.NET MVC要用 CDN上的資源呢,可以參考如下的作法

// BundleTable.EnableOptimizations = true;  表示在 debug mode也要使用 cnd的資源
 BundleTable.EnableOptimizations = true;

//bundles.UseCdn = true;  要存取 cdn的資源
 bundles.UseCdn = true;

//要加上 CDN的來源,一般如果 release mode使用 CDN,debug mode就使用 local的
 bundles.Add(new StyleBundle("~/Content/sweetalsertforbs_css", "//cdn.jsdelivr.net/sweetalert/0.1.2/sweet-alert.min.css").Include(
                       "~/Content/sweet-alert.css"
                       ));

bundles.Add(new ScriptBundle("~/bundles/sweetalsertforbs_js", "//cdn.jsdelivr.net/sweetalert/0.1.2/sweet-alert.min.js").Include(
                       "~/Scripts/sweet-alert.js"));

jsDeliver

參考文件

MVC 4 Part 4 - Bundles and Optimisation

2015年12月29日 星期二

試作 CKEditor 圖片選擇器 ( image upload )




之前的文章有分享過用 MongoDB 和 Asp.Net MVC 寫一個別人分享的 blog程式,裡面有用到 CKEditor (ASP.Net版),因為需要上傳文件,所以也要用 CKFinder,但是我想要的是 google blogspot的方式,點選圖片按鈕時可以出現一個 dialog,可以上傳後再選擇或是從雲端選擇圖片,最重要的是要有多選,但基本的元件好像無法達到多選,所以想自己找出一個方法可以達成的。目前在多選的部份已經達到這樣的需求,作法如下

2014年7月17日 星期四

Asp.net MVC 3, 4, 5的說明

這邊有一篇文章有對不同的版本作說明,如下:

ASP.NET MVC3 Vs MVC4 Vs MVC5

如果要開發 MVC 5的程式,需要 Visual Studio 2012以上的版本才行

2015年6月16日 星期二

使用 moment處理 asp.net MVC回傳的 json string

參考文件:Serializing Dates in JSON 
                    momentjs

在 asp.net MVC Controller or WebAPI回傳 json時,一般會使用 json.NET的 JsonConvert.SerializeObject作處理,如果欄位有日期的話,在 json.NET 4.5的版本就會自動轉成  ISO 8601 standard 的格式,而非微軟那個格式,除非有指定 TimeConverter

JsonConvert.SerializeObject(System.DateTime.Now) ISO格式如右 "2015-06-17T00:00:00Z"
而在前端 javascript在接到時就可以用 moment所提供的功能作轉換如下所示即可

moment(event.start).format( 'YYYY-MM-DD HH:mm:ss' )



2016年7月5日 星期二

ASP.NET Core 1.0 所碰到的問題

Announcing ASP.NET Core 1.0 6/27正式版出爐,因為之前 preview 2的版本在執行時, 載入javascript file時會一直出現 502 bad gateway的訊息,但在 MVC 5就不會出現這種情況,所以就試試安裝正式版的 Core 1.0 。

首先要先更新 VS 2015至  update 3的版本,才可以下載 .NET Core 1.0 for Visual Studio

安裝完之後,之前開發 Core的方案建議重新建立新的。執行後也就沒什麼問題了。
但是發現一個 Nuget元件  RouteJs 執行時出現錯誤(但在 preview 2是正常的),
一個 tool  Docker Tools for Visual Studio 2015 - Preview無法安裝成功 ,

目前正在努力上網找答案中~~~~

2016.07.07 Docker Tools for Visual Studio 2015 - Preview可以安裝成功,只要先安裝 Windows Management Framework 即可。



2015年4月2日 星期四

bootstrap model開啟後馬上又自動關閉

相關文章:Bootstrap Modal immediately disappearing

剛好我就是這個原因,因為我是使用 asp.net mvc開發
不小心使用了 script.render載入了兩次 bootstrap.js,後來拿掉後就沒有這個問題了

2015年9月24日 星期四

asp.net mvc存取 wowza具有安全控管的影音串流

參考文件:How to protect streaming using SecureToken in Wowza Streaming Engine

先說一下當年勇好了,大約在13年前 adobe推出了 flash remoting技術,不只可以連結資料庫而且針對多媒體影音串流的部份也可以連至 adobe media server可以達到視訊的功能,而 flash remoting也就後來變成 Flex。

當時自己可以用 flash remoting開發出有現上會議室的功能,可以即時視訊,白板畫畫、聊天等,當時自己在新竹,還跟在台北的朋友作過測試,蠻不賴的。程式碼目前也還保留著。

回到正題,要如何設定你的 wowza application具有安全存取和 如何在存取,參考文件有詳細的說明。 最主要的作法就是產生 token讓 wowza server確認是否正確。而產生 token string的工作就用一個 class作產生,最後再儲存至 Viewbag變數讓 View可以讀到,這樣就可以了。


還有,文件上一直到到,產生 token的工作千萬不要寫在 javascript程式碼內,因為會被看光光哦。

2015年6月24日 星期三

IIS 7以上如何執行執行檔 exe, bat

參考文件:Foo.cmd won't output lines in process (on website)
                    System.Diagnostics.Process does not work on IIS 7

目前有個需求需要在 Asp.net MVC網頁上按個連結就可以直接把標籤列印出來,所以要使用 Process這個類別

寫法大同小異,如下
                      string command = string .Format("tscgin.exe {0} {1}", "mo_print.ini", fileName);
                   
                    Process p = new Process();
                    // Redirect the output stream of the child process.
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.RedirectStandardOutput = true;
                   p.StartInfo.WorkingDirectory = "c:\somewhere"
                    p.StartInfo.FileName =  "cmd.exe";
                    p.StartInfo.Arguments = "/c " + command;
                    p.Start();
                   string output = p.StandardOutput.ReadToEnd();

                    p.WaitForExit();


但在 IIS和本機(或 Server)目錄需要設定權限的動作,如下
  1. 建一個新目錄存放 exe or bat file,並設定權限 NetworkService或建立一個帳號具有執行、寫入、讀取
  2. 至 IIS 找出應用程式的 Application Pool後點選"進階應用",找到 "識別"選項,使用者選擇 NetworkService或你自己建立的新帳號即可

2016年11月10日 星期四

再見了 Web.config,Asp.Net Core使用了 appsettings.json

官網:Asp.Net Core Configuration

其它連結:
The New Configuration Model in ASP.NET Core
No ConfigurationManager in ASP.NET Core

長久以來,不管是 Asp.Net MVC或是 Form的開發,一定要使用到 Web.config,而在程式碼內就會使用到 ConfigurationManager來取得資料庫連線字串 (ConnectionStrings)或是自定變數,但是在 Core已經不採用這樣的方式了,所以要參考上面的文章,要自訂類別,在 startup.cs內來儲存 appsettings.json的變數,而在 controller才可以利用系統 DI方式取得想要的設定值,雖然一開始可能不太適應,但萬事起頭難,用久就會習慣了。

2022年11月10日 星期四

初學 asp.net code blazor

剛好看到 .Net 7 發佈,剛好可以來學習 Blazor,開發環境為 VS Code,使用 EF Core簡單實作捉取 DB資料含 Sqlite及逺端 SQL Server、使用 dotnet ef dbcontext scaffold產出對應的 DbContext 及 Class物件,Blazor寫的方式很像 asp.net form或是更像我之前寫的 Adobe Flex程式,把程式碼和介面都放在同一個檔案內,跟 MVC的作法有點不同,後續學習後希望可以把更多實作所碰到的問題及解決方式跟大家分享,目前分享文章如下

为何WebAssembly方兴未艾,而JavaApplet/Flex/Silverlight走向消亡?

Blazor 教程 - 生成第一个 Blazor 应用

存取 SQLite 程式範例

現有資料庫產出 DBContext及 table/view class

Blazor Server和Blazor WebAssembly的差異



2016年12月7日 星期三

ASP.NET Core Render View to String in Controller

參考文章:
ASP.NET Core Render View to String
Render a Razor View containing a URL to a string in ASP.NET Core

這個目的是直接在 controller直接產生 partial view的字串再回傳至 view ajax result,然後直接把某個 element換掉或作內文休改,這種方式當然也有其它的方式達成,但直接在 contorller內產生 view string也是可以試試,因為在 Asp.net MVC 5也有類似的作法或是直接使用 westwind的 library。但在 Asp.net Core就不能直接拿來用。所以參考上面的分享文章就可以達到目的了。

2012年12月26日 星期三

ASP.NET Forms Authentication Role-based Security

參考網址 http://www.codeproject.com/Articles/2905/Role-based-Security-with-Forms-Authentication


標準的asp.net 和 MVC都適用

驗證密碼後所要作的事,把資料寫入 cookie中

 FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // Ticket version
Username.Value, // Username associated with ticket
DateTime.Now, // Date/time issued
DateTime.Now.AddMinutes(30), // Date/time to expire
true, // "true" for a persistent user cookie
reader.GetString(0), // User-data, in this case the roles
FormsAuthentication.FormsCookiePath);// Path cookie valid for

// Encrypt the cookie using the machine key for secure transport
string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash); 
     // Set the cookie's expiration time to the tickets expiration time
if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;

// Add the cookie to the list for outgoing response
Response.Cookies.Add(cookie);


最重要的是要在 Global.asax 加上這些程式碼

protected void Application_AuthenticateRequest(Object sender,
EventArgs e)
{
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
FormsIdentity id =
(FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;

// Get our roles from user Data
string userData = ticket.UserData;
string[] roles = userData.Split(',');
HttpContext.Current.User = new GenericPrincipal(id, roles);
}
}
}
}