i am using asp.net core 3.1 this code in used for saving data into database and iIt works fine until i add the code for search it gives this error (The request matched multiple endpoints. Matches:)
this is the controller
namespace Info.Controllers
{
public class DemoController : Controller
{
private readonly ApplicationDbContext _context;
public DemoController(ApplicationDbContext context)
{
_context = context;
}
public IActionResult Index()
{
var result = _context.Files.ToList();
return View(result);
}
[HttpPost]
public IActionResult Index(IFormFile files)
{
if (files != null)
{
if (files.Length > 0)
{
//Getting FileName
var fileName = Path.GetFileName(files.FileName);
//Getting file Extension
var fileExtension = Path.GetExtension(fileName);
// concatenating FileName FileExtension
//var newFileName = String.Concat(Convert.ToString(Guid.NewGuid()), fileExtension);
var newFileName = String.Concat((Guid.NewGuid()), fileExtension);
var objfiles = new Files()
{
DocumentId = 0,
Name = newFileName,
FileType = fileExtension,
};
using (var target = new MemoryStream())
{
files.CopyTo(target);
objfiles.DataFiles = target.ToArray();
}
_context.Files.Add(objfiles);
_context.SaveChanges();
}
}
return RedirectToAction("Index");
}
public async Task<IActionResult> DownloadImage(string filename,int id)
{
if (filename == null)
return Content("filename is not availble");
var file = await _context.Files.Where(x => x.DocumentId == id).FirstOrDefaultAsync();
var path = Path.Combine(Directory.GetCurrentDirectory(), filename);
var memory = new MemoryStream();
{
}
memory.Position = 0;
return File(memory, GetContentType(path), Path.GetFileName(path));
}
private string GetContentType(string path)
{
var types = GetMimeTypes();
var ext = Path.GetExtension(path).ToLowerInvariant();
return types[ext];
}
private Dictionary<string, string> GetMimeTypes()
{
return new Dictionary<string, string>
{
{".txt", "text/plain"},
{".pdf", "application/pdf"},
{".doc", "application/vnd.ms-word"},
{".docx", "application/vnd.ms-word"},
{".xls", "application/vnd.ms-excel"},
{".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
{".png", "image/png"},
{".jpg", "image/jpeg"},
{".jpeg", "image/jpeg"},
{".gif", "image/gif"},
{".csv", "text/csv"}
};
}
public IActionResult Privacy()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Index(string searchString)
{
var FileController = from m in _context.Files
select m;
if (!String.IsNullOrEmpty(searchString))
{
FileController = FileController.Where(s => s.Name!.Contains(searchString));
}
return View(await FileController.ToListAsync());
}
}
}
I've tried modifying the code for search but to no avail
CodePudding user response:
Shouldn't your search be a HttpGet
? if both functions are HttpPost
how will it know which one to call?
CodePudding user response:
I am using asp.net core 3.1 this code in used for saving data into database and iIt works fine until i add the code for search it gives this error (The request matched multiple endpoints. Matches:)
Well, your exceprtion is pretty obvious. Because in asp.net core
regardless of its version deosn't accept same action name with same HTTP VERB
consequently you are getting the exception. I am hoping you got following error:
Exception Reproduced:
Solution:
It can be resolved in many ways, for instance, you can rename your controller action, you can modify the HTTP Verb. If you just rename your public async Task<IActionResult> Index(string searchString)
to Search
instead of Index
or modify the http verb from [HttpPost]
to [HttpGet]
your issue would be resolved.
Output:
Note: You should always keep in mind while define a name for your controller, action, method, or variable for the sake of readability.
* Index - the main "landing" page. This is also the default endpoint.
* Edit - an edit page for the "thing"
* New - a create page for the "thing"
* Create - creates a new "thing" (and saves it if you're using a DB)
* Search- While searching from list or anything.
You can have look on naming convensions guidelines for asp.net core here