Riddhi Valecha

Riddhi Valecha

  • 429
  • 3.2k
  • 396.9k

Debug .NET Core with Entity Framework and Cookie on localhost

Nov 20 2020 1:45 PM

Dear Team,

I have a project on .NET Core MVC Web API with EntityFramewok and MySQL as back-end.

We have a URL "a.b.com" from which we need to take the cookies in class "CustomAuthorization.cs" class file.
 
I am not able to debug the project as I am getting null value in my cookie from localhost.
 
 
Values in URL are -
 
 
Please guide -
1. How to debug the swagger by editing the "hosts" file in folder - C:\Windows\System32\drivers\etc
 
2. We apply "CustomAuthorization.cs" class to all the Controllers. We need to take the DomainID from the URL and insert the database.
 
My Code -
1.Class File For Adding String Values -
public class CPQuestionMaster
{
public readonly ConfigServerData configServer;
public readonly string EmpCode;
public readonly string DomainID;
public readonly HttpRequest httpRequest;
public CPQuestionMaster(ConfigServerData _config, HttpRequest _httpRequest)
{
configServer = _config;
httpRequest = _httpRequest;
this.EmpCode = Convert.ToString(httpRequest.HttpContext.Items["EmpCode"]);
this.DomainID = Convert.ToString(httpRequest.HttpContext.Items["DomainID"]);
}

public String AddMainQuestion(AddCPQuestionMasterModel addQuestionMasterModel)
{
string responseAddQuestion = string.Empty;
Int32 totalquestions = 0;
CPQuestionMasterModel _resReponseQuestionMasterModel = new CPQuestionMasterModel();
CPQuestionMasterContext DBQuestionMasterContext = new CPQuestionMasterContext();
try
{
if (addQuestionMasterModel.Question_Add_list != null)
{
totalquestions = addQuestionMasterModel.Question_Add_list.Count();
if (totalquestions > 0)
{
foreach (var question in addQuestionMasterModel.Question_Add_list)
{
Int32 QuestionMaster_Count = DBQuestionMasterContext.CP_Question_Master.Count();
Int32 ncount = QuestionMaster_Count + 1;
_resReponseQuestionMasterModel.QID = ncount;
_resReponseQuestionMasterModel.Service = addQuestionMasterModel.Service_Add;
_resReponseQuestionMasterModel.Question = question.ToString();
_resReponseQuestionMasterModel.CreatedDate = System.DateTime.Now;
_resReponseQuestionMasterModel.Active = 1;
_resReponseQuestionMasterModel.CreatedBy = DomainID;
if (addQuestionMasterModel.ParentQuestionID_Add.ToString().Length > 0)
_resReponseQuestionMasterModel.FKQID = addQuestionMasterModel.ParentQuestionID_Add;
else
_resReponseQuestionMasterModel.FKQID = 0;
_resReponseQuestionMasterModel.Ratings = addQuestionMasterModel.Ratings_Add;
DBQuestionMasterContext.Add(_resReponseQuestionMasterModel);
DBQuestionMasterContext.SaveChanges();
}
responseAddQuestion = "Total : " + " " + totalquestions + " " + " Questions are successfully registered.";
}
else
{
responseAddQuestion = "Minimum one question is required.";
}
}
else
{
responseAddQuestion = "Minimun one question is required.";
}
}
catch (Exception err)
{
responseAddQuestion = "Error In Saving Record.";
}
return responseAddQuestion;
}
}
---------------------------
Controller Code -
 

[ApiController]

[ServiceFilter(typeof(CustomAuthorization))]

public class QuestionMasterController : ControllerBase

{

private ConfigServerData configServer;

public QuestionMasterController(IOptionsSnapshot<ConfigServerData> config)

{

configServer = config.Value;

if (!string.IsNullOrEmpty(configServer.Database))

Startup.ConnectionString = configServer.Database;

}

[Route("AddAdminQuestionMaster")]

[HttpPost]

public IActionResult Add(AddCPQuestionMasterModel ReqQuestionMaster)

{

Response responseAdd = new Response();

try

{

responseAdd.Message = new DAL.CPQuestionMaster(configServer,Request).AddMainQuestion(ReqQuestionMaster);

responseAdd.Code = (int)HttpStatusCode.OK;

}

catch (Exception ex)

{

throw new CustomException(ex.Message, ex);

}

return StatusCode(responseAdd.Code, responseAdd);

}

-----------
Please guide - where I am going wrong. 

Answers (1)