顯示具有 MVC 標籤的文章。 顯示所有文章
顯示具有 MVC 標籤的文章。 顯示所有文章

2016年12月14日 星期三

關於Attribute Routing,使用正則表示式排除特定字串

MVC架構下,使用Attribute Routing時,遇到要使用正則表示式排除有特定字串的Route。

當Model中使用  [RoutePrefix("{Type:regex(Apple|Pen)}")] 

表示當Route為ApplePen的時候,就會進入此Model
EX(http://localhost:XXXX/Apple或http://localhost:XXXX/Pen)

Type:regex(可以使用正則表示式來設定想要的字串內容)
  1. ^((?!特定字串).)*$
  2. ^(.(?<!特定字串))*$ 
      例如:^((?!Apple).)*$,只會取得不包含"Apple"的字串。

合起來使用就可以達到反效果,表示當Route中不含ApplePen的時候,就會進入此Model
EX:[RoutePrefix("{Type:regex( ^((?!Apple|).Pen)*$)}")]

--------------------------------------------------------------------------------------------------------------------------
這邊順帶做個筆記,範例:(http://localhost:XXXX/Sample/TestString123/456/789/123)

[RoutePrefix("Sample")]
[Route("TestString{Command}/{*pathInfo}")]

TestString為設定的特定字串,這樣就可以取得Command的值,此為123。
pathInfo可取得後續所有Route,此為456/789/123。

2016年11月21日 星期一

ASP.NET 有用過的using整理(持續更新)

using System.Threading.Tasks;
using System.Globalization;
using System.Threading;
using System.Configuration;
using Newtonsoft.Json;
using System.Net.Http;
using System.Xml.Serialization;
using System.IO;
using System.Web;
using System.Web.Routing;

以下為MVC架構才能使用(或是加入參考)
using System.Web.Mvc;
using System.Web.Helpers;

以下需從NuGet安裝
using Microsoft.Azure.Devices.Common;
using Microsoft.AspNet.SignalR;
using log4net;

2016年11月16日 星期三

ASP MVC結構下,如何在.js中讀取Web.config

  1. 在Web.config中放入需要讀取的值,例如123456。
  2. <appsettings>
        <add key="testjsget" value="123456"></add>
    </appsettings>
    
  3. 在對應.js的.cshtml中放入。
  4. var js =
    {
        get: '@System.Configuration.ConfigurationManager.AppSettings["testjsget"]'
    };
    
  5. 最後是.js中讀取值123456,在這裡使用彈出視窗顯示。
  6. alert(js.get);