簡體   English   中英

使用LINQ查詢單字符串導致路由

[英]Using LINQ query single string result in routing

我現在已經嘗試了兩天,分別經過嘗試,錯誤和Google,以執行以下操作:

當用戶進入我的MVC站點時,它將使用其Windows帳戶登錄。 運作正常。 當他這樣做時,MVC連接到db以提取他的名字(基於他的帳戶),這也很好用。 我的任務是使“索引”頁面默認包含過濾的信息。 但是用戶還必須能夠自己更改這些過濾器。 使用url中的參數,我的網站的搜索功能運行良好。 我試圖通過直接輸入用戶名,然后將其設置為Parameter.Optional來編輯路由。 兩種方法都會導致錯誤:

public class RouteConfig
{
    public static CaseDBContext db = new CaseDBContext();
    public static string UName()
    {
        return db.Users.AsQueryable().Where(col => col.UNAD == HttpContext.Current.User.Identity.Name.ToString()).Select(col => col.Users).ToList().SingleOrDefault().ToString();
    }
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "",
            url: "{controller}/{action}/{dropDownIssBy}",
            defaults: new { controller = "Cases", action = "Index", dropDownIssBy = UName(), caseStatus = UrlParameter.Optional, sortBy = UrlParameter.Optional }
        );

這以null引用異常結束。 如果我使用新的Func(UName),則將加載站點,但是在調試時,它根本不會觸發UName函數,而只會忽略它。

private CaseDBContext db = new CaseDBContext();
public string UName()
{
return db.Users.AsQueryable().Where(col => col.UNAD == User.Identity.Name.ToString()).Select(col => col.Users).ToList().SingleOrDefault().ToString();
}
    public ActionResult Index(int? page, [...parameters...], string caseStatus = "In Progress", string dropDownIssBy = UName(), string SortBy = "Issue Date asc")
{
code here
}

“默認參數值”的高位結果必須是編譯時常數。

我也試過這個:

public static string UName(string i)
    {
        string str = db.Users.AsQueryable().Where(col => col.UNAD == i).Select(col => col.Users).ToList().SingleOrDefault().ToString();
        return str;
    }
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "",
            url: "{controller}/{action}/{dropDownIssBy}",
            defaults: new { controller = "Cases", action = "Index", dropDownIssBy = UName(HttpContext.Current.User.Identity.Name.ToString()), caseStatus = UrlParameter.Optional, sortBy = UrlParameter.Optional }
        );

結果是“對象引用未設置為對象的實例”。

僅供參考:第一個代碼連接到第一個提到的試用。 第二個代碼依賴於不同的RouteConfig設置(不是字符串dropDownIssBy​​ = UName,而是字符串dropDownIssBy​​ = Parameter.Optional)。

我不太擅長解決這些路由問題。 我確實需要應用LINQ查詢以實現所需的功能。 您能給我一些解決問題的建議嗎?

提前致謝!

在兩種情況下,您都試圖將方法UName分配給字符串dropDownIssBy​​(而不是結果)。 您錯過了括號。 即使用UName()

有問題的行應顯示為:

defaults: new { controller = "Cases", action = "Index", dropDownIssBy = UName(), caseStatus = UrlParameter.Optional, sortBy = UrlParameter.Optional }

public ActionResult Index(int? page, [...parameters...], string caseStatus = "In Progress", string dropDownIssBy = UName(), string SortBy = "Issue Date asc")

如果您確實打算傳遞一個方法,那么第一行的語法將更像:

defaults: new { controller = "Cases", action = "Index", dropDownIssBy = new Func<string>(UName), caseStatus = UrlParameter.Optional, sortBy = UrlParameter.Optional }

對於第二行,我認為您不能為可選參數設置默認方法。 您可以通過將默認值設置為null來解決此問題,並在方法為null時從方法中調用UName。 例如

public ActionResult Index(int? page, [...parameters...], string caseStatus = "In Progress", Func<string> dropDownIssBy = null, string SortBy = "Issue Date asc")
{
    if(dropDownIssBy == null)
    {
        dropDownIssBy = UName;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM