asp.net 자습서에서 FileUpload 샘플을 사용하고 있습니다. 독립형으로 제작하면 잘 작동합니다. 그러나 새 MVC4 웹 사이트에 해당 기능을 추가하려고 할 때마다 라우팅이 잘못되었습니다. 아마 이것을 잘 설명하는, 그래서 여기에 코드입니다 아니에요 : 내가 직접 localhost:13927api/upload
로 이동하면Html 양식 - 경로가 잘못되었습니다
<div style="height:400px;">
<h3>File Upload</h3>
<form name="trip_search" method="post" enctype="multipart/form-data" action="api/upload">
<div>
<input type="radio" name="trip" value="round-trip"/>
Round-Trip
</div>
<div>
<input type="radio" name="trip" value="one-way"/>
One-Way
</div>
<div>
<input type="checkbox" name="options" value="nonstop" />
Only show non-stop flights
</div>
<div>
<input type="checkbox" name="options" value="airports" />
Compare nearby airports
</div>
<div>
<input type="checkbox" name="options" value="dates" />
My travel dates are flexible
</div>
<div>
<label for="seat">Seating Preference</label>
<select name="seat">
<option value="aisle">Aisle</option>
<option value="window">Window</option>
<option value="center">Center</option>
<option value="none">No Preference</option>
</select>
</div>
<div>
<input type="submit" value="Submit" />
</div>
</form>
내가 참조 : 여기
[HttpPost]
public async Task<HttpResponseMessage> PostFile()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
try
{
var sb = new StringBuilder(); // Holds the response body
// Read the form data and return an async task.
await Request.Content.ReadAsMultipartAsync(provider);
// This illustrates how to get the form data.
foreach(var key in provider.FormData.AllKeys)
{
var strings = provider.FormData.GetValues(key);
if (strings != null) foreach(var val in strings)
{
sb.Append(string.Format("{0}: {1}\n", key, val));
}
}
// This illustrates how to get the file names for uploaded files.
foreach(var file in provider.FileData)
{
var fileInfo = new FileInfo(file.LocalFileName);
sb.Append(string.Format("Uploaded file: {0} ({1} bytes)\n", fileInfo.Name, fileInfo.Length));
}
return new HttpResponseMessage
{
Content = new StringContent(sb.ToString())
};
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
내가 사용하는 페이지입니다 웹 API 메소드의 응답. 내 WebApiConfig에 DefaultApi 경로가 등록되어 있습니다.
하지만 내가 localhost/Home/About
페이지에 있는데 제출 버튼을 클릭하면 localhost/Home/api/upload
으로 이동하려고 시도합니다. 존재하지 않습니다.
무엇이 누락 되었습니까?
편집
마리오에 의해 제안은 내 문제를 해결. 내 양식의 조치 메소드는 루트와 관련이 없습니다.
action="api/upload" vs. action="/api/upload"
그게 내 문제를 해결합니다. (- 즉 기본 경우> 말 yoursite/홈/색인), 다음 작업 = "API/시켜라"는 작동 기본 경로에있을 때
:
문제에 대한 정교의 비트가
현재 경로는 여전히 웹 사이트의 루트로 간주되기 때문입니다. 그러나 실제로 경로 (예 : yoursite/Home/About)로 이동하면 현재 경로가 "집"아래에 있으므로 누락 된 "/"는 자연적으로 루트가 아닌 현재 경로와 관련이 있습니다. 이것이 문제의 뷰가 기본 뷰이므로 선행 "/"없이 샘플이 작동하는 이유입니다.양식 action="api/upload"
이 루트에 상대적인되지 않습니다 : 그것은 너무 다른 사람을 도울 수 있다면
'action = "api/upload"양식은 루트와 관련이 없습니다. 'action = "/ api/upload"를 시도하거나 헬퍼 중 하나를 사용하여 경로를 지정하십시오. –
@ Html.BeginForm을 사용하여 컨트롤러를 지정할 수 있습니다. 잘못된 것은 현재 양식 작업으로 집을 컨트롤러로 보냅니다. – Maess
@Mario - 오, 좋은 슬픔 ... 뿌리에 비례하여, 어떻게 생각하지 않았습니까? 이제 작동합니다. – whipdancer