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

2015年3月30日 星期一

瀏覽器Cache問題

JavaScript或CSS修改後,網站老是抓到舊的檔案

1.使用ASP.NET MVC可以用bundles方法,檔案後方會出現v=XXXXXXX,只要檔案改內容xxxxxx則會不同。

注意:如果使用bundles,Web.config的compilation 設定debug="true"則會無效(網站發行後則不會有debug),得另行加入BundleTable.EnableOptimizations = true;

2.使用小技巧,強制每次JavaScript檔案後面都會有版本。

2015年1月27日 星期二

X-Frame-Options 錯誤

在網站裡面iFrame嵌入ASP.NET MVC5專案的網頁,會出現X-Frame-Options的安全性錯誤(此內容無法在框架中顯示)
解決方式: 覆寫此設定在Global.asax.cs

AntiForgeryConfig.SuppressXFrameOptionsHeader = true;

2014年10月24日 星期五

使用 FormsAuthentication.SetAuthCookie

使用FormsAuthentication.SetAuthCookie

var acct = "spring";
FormsAuthentication.SetAuthCookie(acct, false);

2014年10月22日 星期三

ASP.NET MVC Web API 資料庫處理 using

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Net.Http.Formatting;
using System.Data;
using MvcMusicStore.Models;
using System.Data.Entity;
using System.Configuration;
using System.Data.SqlClient;

namespace MvcMusicStore.Controllers
{
    public class ExtJSApiController : ApiController
    {     
        public dynamic ReviewDataGet(FormDataCollection form)
        {
            string conn = ConfigurationManager.ConnectionStrings["MvcMusicStoreContext"].ConnectionString;
            DataTable dt = new DataTable();
            string strCheck = form.Get("x");
            string strAlbumID = form.Get("AlbumID");
            string strTitle = form.Get("Title");
            using (SqlConnection sqlConnection = new SqlConnection(conn))
            {
                string strSql = "SELECT * FROM Albums WHERE AlbumID LIKE @AlbumID AND Title LIKE @Title";

                SqlCommand sqlCommand = new SqlCommand(strSql, sqlConnection);
                sqlCommand.Parameters.AddWithValue("@AlbumID", "%" + strAlbumID + "%");
                sqlCommand.Parameters.AddWithValue("@Title", "%" + strTitle + "%");
                sqlConnection.Open();
                dt.Load(sqlCommand.ExecuteReader());
            }
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            return new ApiResponse()
            {
                success = true,
                msg = "",
                ds = ds
            };
        }
        public dynamic AlbumComboGet()
        {
            DataTable dt = new DataTable();
            using (SqlConnection sqlConnection = new SqlConnection(conn))
            {
                string strSql = "SELECT AlbumID AS 'TEXT',AlbumID AS 'VALUE' FROM Albums";
                SqlCommand sqlCommand = new SqlCommand(strSql, sqlConnection);
                sqlConnection.Open();
                dt.Load(sqlCommand.ExecuteReader());
            }
            return dt;
        }
    }
    public class ApiResponse
    {
        public DataSet ds { get; set; }
        public string msg { get; set; }
        public bool success { get; set; }
    }
}


2014年10月6日 星期一

ASP.NET MVC 使用 FullCalendar

好用的Calendar   FullCalendar


Model

public class CALENDAR
{
  public string title { get; set; }
  public string start { get; set; }
  public string end { get; set; }
  public string url { get; set; }
}

View

@{
    ViewBag.Title = "Calendar";
}

@section featured {

<link href="@Url.Content("~/Scripts/fullcalendar/lib/cupertino/jquery-ui.min.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Scripts/fullcalendar/fullcalendar.css")" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="@Url.Content("~/Scripts/fullcalendar/lib/moment.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/fullcalendar/lib/jquery.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/fullcalendar/fullcalendar.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/fullcalendar/lang-all.js")"></script>

<style>
    body {
        margin: 40px 10px;
        padding: 0;
        font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif;
        font-size: 14px;
        overflow:scroll !important;
    }

    #calendar {
        max-width: 900px;
        margin: 0 auto;
    }
</style>

   <div id='calendar'></div>
<script>

    $(document).ready(function () {
        $('#calendar').fullCalendar({
            theme: true,
            lang: 'zh-tw',
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
            },
            editable: false,
            eventLimit: true, // allow "more" link when too many events
            events: {
                type: 'POST',
                url: '../api/TP1025CalendarGet/CalendarData',
                error: function () {
                    alert('資料存取失敗!');
                },
                success: function (response) {
 
                }
            }
        });

    });

</script>
}

Controller

public class TP1025CalendarController : Controller
{
  //
  // GET: /TP1025Calendar/
  public ActionResult Index()
  {
      return View();
  }
}

Web API

public class TP1025CalendarGetController : ApiControllerWebBase
{
    public List CalendarData(FormDataCollection form)
    {
        List calendar = new List();

        foreach (DataRow dr in response.ds.Tables[0].Rows)
        {
            calendar.Add(new CALENDAR { title = dr["title"].ToString(), start = dr["start"].ToString(), url = "" });
        }
        return calendar;
    }
}

2014年9月14日 星期日

ASP.NET MVC Route Attribute


Route屬性

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace MvcMusicStore.Controllers
{
    public class DemoRouteController : ApiController
    {
        //GET: api/DemoRoute/5
        [Route("customers/{id}/orders")]
        [HttpGet]
        public string FindValue(int id)
        {
            return id.ToString();
        }
    }
}

ASP.NET MVC (Model, View, Controller 簡單範例)

Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1.Models
{
    public class Album
    {
        public int AlbumID { get; set; }
        public string Title { get; set; }
    }
}

List Controller

public ActionResult Index()
        {
            List albums = new List();
            albums.Add(new Album { AlbumID = 1, Title = "Sting" });
            albums.Add(new Album { AlbumID = 2, Title = "Joan" });
            return View(albums);
        }

List View

@model IEnumerable<WebApplication1.Models.Album>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.AlbumID }) |
            @Html.ActionLink("Details", "Details", new { id=item.AlbumID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.AlbumID })
        </td>
    </tr>
}

</table>

Detail Controller

public ActionResult Details(int id)
        {
            var album = new Album { AlbumID = id, Title = "Hello" };
            return View(album);
        }

Detail View

@model WebApplication1.Models.Album

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<div>
    <h4>Album</h4>
 <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.Title)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Title)
        </dd>

    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.AlbumID }) |
    @Html.ActionLink("Back to List", "Index")
</p>