مرجع تخصصی برنامه نویسان

انجمن تخصصی برنامه نویسان فارسی زبان

کاربر سایت

ofoghcomputer

عضویت از 1393/07/15

رفع پیغام no suitable method found to override error

  • یکشنبه 26 اردیبهشت 1395
  • 21:25
تشکر میکنم

با سلام.

من یک Custom Role Provider ساختم.

هنکامی که می خواهم یک تابع جدید بنویسم با پیغام suitable method found to override error رو برو می شوم.

لطفا راهنمایی کنید

پاسخ های این پرسش

تعداد پاسخ ها : 9 پاسخ
کاربر سایت

افشین ایمانی زاده

عضویت از 1393/12/24

  • یکشنبه 26 اردیبهشت 1395
  • 22:22

با سلام

دوست عزیز زمانی که قصد دارید یک Provider اختصاصی بنویسید باید تمام متد های abstract رو override کنید.

باید تمام متدهای زیر رو در آن کلاس Custom خود داشته باشید

 public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] GetRolesForUser(string username)
        {
            throw new NotImplementedException();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }

 

کاربر سایت

ofoghcomputer

عضویت از 1393/07/15

  • دوشنبه 27 اردیبهشت 1395
  • 06:06

با عرض سلام و تشکر از پاسخ شما

تمام این متدها وجود دارد.

می خواهم یک متد جدید به آن اضافه کتم

لطفا راهنمایی کنید

کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • دوشنبه 27 اردیبهشت 1395
  • 08:18

برای اضافه کردن متد جدید هیچ مشکلی وجود ندارد

شاید در نوع و یا بدنه کدتون مشکلی وجود دارد

کدتون رو قرار دهید تا بررسی کنیم

کاربر سایت

ofoghcomputer

عضویت از 1393/07/15

  • دوشنبه 27 اردیبهشت 1395
  • 10:38

با تشکر

کد زیر مربوط به custom role provider هست:

using System;
using System.Collections.Generic;
using System.Configuration.Provider;
using System.Linq;
using System.Web;
using System.Web.Security;
using Talash.Models;
using System.Web.Mvc;
namespace Talash
{
    public class CustomRoleProvider : RoleProvider
    {
        talasheduEntities talash = new talasheduEntities();
        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }

            set
            {
                throw new NotImplementedException();
            }
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override void CreateRole(string roleName)
        {
            if (RoleExists(roleName))
            {
                throw new ProviderException("Role name already exists.");
            }
            else
            {
                Tbl_Role r = new Tbl_Role();
                r.RoleName = roleName;
                talash.Tbl_Role.Add(r);
                talash.SaveChanges();
            }//throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            Tbl_Role r = talash.Tbl_Role.First(i=>i.RoleName==roleName);
            talash.Tbl_Role.Remove(r);

            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            string[] f = { "" };
            var d = (from u in talash.Tbl_Role select new { u.RoleName });
            for (int i = 0; i < d.Count(); i++)
            {
                f[i] = d.ToArray()[i].RoleName;
            }
            return f;
        }

        public override string[] GetRolesForUser(string username)
        {
            string[] f = {""};
            List<string> userRoles = new List<string>();
            var d = from u in talash.Tbl_User
                     join k in talash.Tbl_RoleInUser on u.UserID equals k.UserID
                     join r in talash.Tbl_Role on k.RoleID equals r.RoleID
                     where u.MelliCode== username
                     select new { r.RoleName };
            for (int i = 0; i < d.Count(); i++)
            {
                userRoles.Add(d.ToArray()[i].RoleName.Trim());
            }
            return userRoles.ToArray();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            bool userIsInRole = false;
            var userID = talash.Tbl_User.SingleOrDefault(u => u.MelliCode == username);
            var rolename = (from r in talash.Tbl_Role
                            join ru in talash.Tbl_RoleInUser on r.RoleID equals ru.RoleID
                            where ru.UserID == userID.UserID && r.RoleName == roleName
                            select new { r.RoleName }).ToArray();
            if (rolename.Count() > 0)
            {
                userIsInRole = true;
            }
            return userIsInRole;
        }

     

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            bool exists = false;
            var role = from r in talash.Tbl_Role where r.RoleName == roleName select r.RoleID;
            if (role.Count()>0)
            {
                exists = true;
            }
            return exists;
           
        }
    }
}

متد زیر را که اضافه می کنم روی این متد ارور می گیرد و پیغامی که گفتم می دهد:

public override bool IsUser(string username)
        {
            string[] f = { "" };
            bool chk = false;
            List<string> userRoles = new List<string
            return chk;
        }

لطفا راهنمایی کنید

کاربر سایت

افشین ایمانی زاده

عضویت از 1393/12/24

  • دوشنبه 27 اردیبهشت 1395
  • 10:48

دوست عزیز کلمه کلیدی override رو از متدتون حذف کنید

کاربر سایت

ofoghcomputer

عضویت از 1393/07/15

  • دوشنبه 27 اردیبهشت 1395
  • 11:50

دوست عزیز.

حذف کردم درست شد.

مشکل اینه که وقتی Roles رو می نویسم متد جدیدی که ساخته شده تو لیست متدهاش نیست

لطفا راهنمایی کنید

کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • دوشنبه 27 اردیبهشت 1395
  • 12:01
public static bool IsUser(string username)
        {
            string[] f = { "" };
            bool chk = false;
            List<string> userRoles = new List<string
            return chk;
        }

 

کاربر سایت

ofoghcomputer

عضویت از 1393/07/15

  • دوشنبه 27 اردیبهشت 1395
  • 12:10

جناب مدائنی

مشکل جدید اینه که وقتی Roles رو می نویسم متد جدیدی که ساخته شده تو لیست متدهاش نیست

لطفا راهنمایی کنید

کاربر سایت

افشین ایمانی زاده

عضویت از 1393/12/24

  • دوشنبه 27 اردیبهشت 1395
  • 13:04

دوست عزیز متد رو به صورت زیر تعریف کنید

 public bool IsUser(string username)
        {
            string[] f = { "" };
            bool chk = false;
            List<string> userRoles = new List<string>();
            return chk;
        }

و در برنامه به صورت زیر استفاده کنید

var CustomProvider = Roles.Provider as CustomRoleProvider;
            CustomProvider.IsUser("UserName");

 

کاربرانی که از این پست تشکر کرده اند

هیچ کاربری تا کنون از این پست تشکر نکرده است

اگر نیاز به یک مشاور در زمینه طراحی سایت ، برنامه نویسی و بازاریابی الکترونیکی دارید

با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)